C#.NET 连接 Access操作类

1、配置web.config文件:配置数据库连接参数

<configuration>
 <appSettings/>
 <connectionStrings>
 
  <add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:/TeacherSystem/App_Data/db.mdb;Jet OLEDB:Database Password=123"
   providerName="System.Data.OleDb" />
 </connectionStrings>

 2

程序设计开始:

1、按要求建立Access数据库及数据表

2、编写数据库访问、操作的公用类,此类可以复用到以后开发的任何系统中

(1)、新建一个C# 类库项目, 命名为“Com.LXJ”,设置项目属性:程序集名称、默认命名空间均为“Com.LXJ”

(2)、在此项目目录下创建目录Database,新建C# 类文件ConnDbForAccess.cs 在Database目录下。

              添加引用:System.Web.dll

(3)、编写ConnDbForAccess.cs 的代码

  using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Configuration;

namespace Com.LXJ.Database
{
    /// <summary>
    /// conn 的摘要说明。
    /// </summary>
    public class ConnDbForAcccess
    {
        /// <summary>
        /// 连接数据库字符串
        /// </summary>
        private string connectionString;

        /// <summary>
        /// 存储数据库连接(保护类,只有由它派生的类才能访问)
        /// </summary>
        protected OleDbConnection Connection;

        /// <summary>
        /// 构造函数:数据库的默认连接
        /// </summary>
        public ConnDbForAcccess()
        {
            string connStr;
            connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
           // connStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString(); //从web.config配置中读取
            connectionString = connStr;
            //connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + connStr;
           // connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
            //
            Connection = new OleDbConnection(connectionString);
        }

        /// <summary>
        /// 构造函数:带有参数的数据库连接
        /// </summary>
        /// <param name="newConnectionString"></param>
        public ConnDbForAcccess(string newConnectionString)
        {
            //connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + newConnectionString;
            connectionString = newConnectionString;
            Connection = new OleDbConnection(connectionString);
        }

        /// <summary>
        /// 获得连接字符串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
        }


        /// <summary>
        /// 执行SQL语句没有返回结果,如:执行删除、更新、插入等操作
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns>操作成功标志</returns>
        public bool ExeSQL(string strSQL)
        {
            bool resultState = false;

            Connection.Open();
            OleDbTransaction myTrans = Connection.BeginTransaction();
            OleDbCommand command = new OleDbCommand(strSQL, Connection, myTrans);

            try
            {
                command.ExecuteNonQuery();
                myTrans.Commit();
                resultState = true;
            }
            catch
            {
                myTrans.Rollback();
                resultState = false;
            }
            finally
            {
                Connection.Close();
            }
            return resultState;
        }

        /// <summary>
        /// 执行SQL语句返回结果到DataReader中
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns>dataReader</returns>
        private OleDbDataReader ReturnDataReader(string strSQL)
        {
            Connection.Open();
            OleDbCommand command = new OleDbCommand(strSQL, Connection);
            OleDbDataReader dataReader = command.ExecuteReader();
            Connection.Close();

            return dataReader;
        }

        /// <summary>
        /// 执行SQL语句返回结果到DataSet中
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns>DataSet</returns>
        public DataSet ReturnDataSet(string strSQL)
        {
            Connection.Open();
            DataSet dataSet = new DataSet();
            OleDbDataAdapter OleDbDA = new OleDbDataAdapter(strSQL, Connection);
            OleDbDA.Fill(dataSet, "objDataSet");

            Connection.Close();
            return dataSet;
        }

        /// <summary>
        /// 执行一查询语句,同时返回查询结果数目
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns>sqlResultCount</returns>
        public int ReturnSqlResultCount(string strSQL)
        {
            int sqlResultCount = 0;

            try
            {
                Connection.Open();
                OleDbCommand command = new OleDbCommand(strSQL, Connection);
                OleDbDataReader dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    sqlResultCount++;
                }
                dataReader.Close();
            }
            catch
            {
                sqlResultCount = 0;
            }
            finally
            {
                Connection.Close();
            }
            return sqlResultCount;
        }


    }//
}//


 好了,数据库访问、数据库操作的公用类完成了,详细代码意义大家自己看吧,这些属于C# 语法知识了,现在可以编译生成项目了,编译完后把项目的dll文件Com.LXJ.dll拷贝到Example项目下的bin目录中,然后在项目Example中引入Com.LXJ.dll即可。稍后介绍如何使用它,休息一下,喝口水,呵呵.....

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值