常用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();
            }
        }
    }
}

 

posted @ 2015-06-17 16:21  Yao,Mane  阅读(1068)  评论(0编辑  收藏  举报