C#操作Access数据库的DBHelper类

1 篇文章 0 订阅
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace InfoManger.MobDbHelper
{
    //本DBHelper适用于ACCESS
    //如果每次执行完后关闭conn,会导致程序卡顿,建议在程序使用期间不要关闭conn
    //每次使用都会执行try{} catch{},容错能力比较强
    class DbHelper
    {
        public static OleDbConnection conn = null;

        private static void InitConnection()
        {
            try
            {
                if (conn == null)
                    conn = new OleDbConnection(ConnectionString.StrCnn);

                if (conn.State == ConnectionState.Broken)
                {
                    conn.Close();
                    conn.Open();
                }

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
            catch (OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
                conn.Close();
            }
        }

        //获取DataReader
        public static OleDbDataReader GetOleDbDataReader(string str)
        {
            InitConnection();
            try
            {
                OleDbCommand cmd = new OleDbCommand(str, conn);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
                return null;
            }

        }

        //查询,获取DataSet
        public static DataSet GetDataSet(string sqlStr)
        {
            InitConnection();

            try
            {
                DataSet ds = new DataSet();
                OleDbDataAdapter dap = new OleDbDataAdapter(sqlStr, conn);
                dap.Fill(ds);
                return ds;
            }
            catch (OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
                return null;
            }
            //conn.Close();
        }

        //查询,获取DataTable
        public static DataTable GetDataTable(string sqlStr)
        {
            try
            {
                return GetDataSet(sqlStr).Tables[0];
            }
            catch (OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
                return null;
            }
        }

        //增删改
        public static int ExecuteNonQuery(string sqlStr)
        {
            InitConnection();

            try
            {
                OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
                int result = cmd.ExecuteNonQuery();
                //conn.Close();
                return result;
            }
            catch (OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
                return 0;
            }
        }

        //执行集合函数
        public static object ExecuteScalar(string sqlStr)
        {
            InitConnection();

            try
            {
                OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
                object result = cmd.ExecuteScalar();
                //conn.Close();
                return result;
            }
            catch (OleDbException Ex)
            {
                MessageBox.Show(Ex.Message);
                return null;
            }
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值