C# OleDBHelper(数据库访问公共接口)

本文介绍了一个用于访问Microsoft Access数据库的OleDb帮助类库,包括连接设置、打开和关闭数据库连接、执行SQL语句、获取数据集和数据表等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace OleDbHelper_TEST
{
    public class OleDbHelper
    {
        public OleDbHelper() { }

        private static OleDbConnection Conn;
        private static OleDbCommand Cmd;
        private static OleDbDataAdapter Da;
        private static DataSet Ds;
        private static DataTable Dt;
        private static string strConn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=c:\\a.mdb";

        public static void setConn(string FileName)
        {
            strConn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source="+FileName;
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        public static void Open()
        {
            Conn = new OleDbConnection();
            Cmd = new OleDbCommand();
            if (Conn.State.Equals(ConnectionState.Closed))
            {
                Conn.ConnectionString = strConn;
                Conn.Open();
            }
            Cmd.Connection = Conn;
        }


        /// <summary>
        /// 关闭连接
        /// </summary>
        public static void Close()
        {
            if (Conn.State.Equals(ConnectionState.Open))
            {
                Conn.Close();
                Conn.Dispose();
            }
        }

        /// <summary>
        /// 执行ExecuteNonQuery()
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public static int ExecuteCmd(string sql)
        {
            try
            {
                Open();
                Cmd.CommandText = sql;
                return Cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            finally
            {
                Close();
            }
        }


        /// <summary>
        /// DataSet类
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public static DataSet GetDataSet(string sql)
        {
            try
            {
                Open();
                Cmd.CommandText = sql;
                Da = new OleDbDataAdapter();
                Da.SelectCommand = Cmd;
                Ds = new DataSet();
                Da.Fill(Ds);
                return Ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            finally
            {
                Close();
            }
        }

        /// <summary>
        /// DataTable 类
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql)
        {
            try
            {
                Open();
                Cmd.CommandText = sql;
                Da = new OleDbDataAdapter();
                Da.SelectCommand = Cmd;
                Dt = new DataTable();
                Da.Fill(Dt);
                return Dt;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            finally
            {
                Close();
            }
        }

        /// <summary>
        /// 执行 ExecuteScalar
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql)
        {
            try
            {
                Open();
                Cmd.CommandText = sql;
                return Cmd.ExecuteScalar();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
            finally
            {
                Close();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值