經常使用ADO.NET操做ACCESS數據庫

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;//
using System.Data.OleDb;//
using System.Drawing;//

namespace EColor
{
    class ControlDBS
    {
        //定義連接字符串
        private OleDbConnection con;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="path">數據庫路徑</param>
        public ControlDBS(string path)
        {
            con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
        }

        /// <summary>
        /// 獲取sql查詢數據到數據表
        /// </summary>
        /// <param name="SQL">SQL語句</param>
        /// <returns>數據表</returns>
        public DataTable GetDataTable(string SQL)
        {
            OleDbDataAdapter adp = new OleDbDataAdapter(SQL, con);
            try
            {
                con.Open();
                DataTable dt = new DataTable();
                adp.Fill(dt);
                return dt;
            }
            catch
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 獲取數據到數據集
        /// </summary>
        /// <param name="SQL">SQL語句</param>
        /// <returns>數據集</returns>
        public DataSet GetDataSet(string SQL)
        {
            OleDbDataAdapter adp = new OleDbDataAdapter(SQL, con);
            try
            {
                con.Open();
                DataSet ds = new DataSet();
                adp.Fill(ds);
                return ds;
            }
            catch
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 獲取數據到DataReader
        /// </summary>
        /// <param name="SQL">SQL語句</param>
        /// <returns>DataReader</returns>
        public OleDbDataReader GetDataReader(string SQL)
        {
            OleDbCommand cmd = new OleDbCommand(SQL, con);
            try
            {
                con.Open();
                OleDbDataReader dr = cmd.ExecuteReader();
                return dr;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 返回sql查詢的第一行第一列
        /// </summary>
        /// <param name="SQL">SQL語句</param>
        /// <returns>第一行第一列的字符串</returns>
        public string GetFirstValue(string SQL)
        {
            OleDbCommand cmd = new OleDbCommand(SQL, con);
            try
            {
                con.Open();
                string strchar = cmd.ExecuteScalar().ToString();
                return strchar;
            }
            catch
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 執行SQL語句
        /// </summary>
        /// <param name="SQL">SQL語句</param>
        public void RunSqlCmd(string SQL)
        {
            OleDbCommand cmd = new OleDbCommand(SQL, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch
            { }
            finally
            {
                con.Close();
            }
        }
    }
}
相關文章
相關標籤/搜索