Home > Database > Mysql Tutorial > body text

c#.net连接access操作类 (转)

WBOY
Release: 2016-06-07 15:49:29
Original
1092 people have browsed it

2 程序设计开始: 1、按要求建立 Access 数据库及数据表 2、编写数据库访问、操作的公用类,此类可以复用到以后开发的任何系统中 (1)、新建一个C# 类库项目, 命名为“Com.LXJ”,设置项目属性:程序集名称、默认命名空间均为“Com.LXJ” (2)、在此项目

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
{
    ///


    /// conn 的摘要说明。
    ///

    public class ConnDbForAcccess
    {
        ///
        /// 连接数据库字符串
        ///

        private string connectionString;

        ///


        /// 存储数据库连接(保护类,只有由它派生的类才能访问)
        ///

        protected OleDbConnection Connection;

        ///


        /// 构造函数:数据库的默认连接
        ///

        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);
        }

        ///


        /// 构造函数:带有参数的数据库连接
        ///

        ///
        public ConnDbForAcccess(string newConnectionString)
        {
            //connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + newConnectionString;
            connectionString = newConnectionString;
            Connection = new OleDbConnection(connectionString);
        }

        ///


        /// 获得连接字符串
        ///

        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
        }

        ///


        /// 执行SQL语句没有返回结果,如:执行删除、更新、插入等操作
        ///

        ///
        /// 操作成功标志
        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;
        }

        ///


        /// 执行SQL语句返回结果到DataReader中
        ///

        ///
        /// dataReader
        private OleDbDataReader ReturnDataReader(string strSQL)
        {
            Connection.Open();
            OleDbCommand command = new OleDbCommand(strSQL, Connection);
            OleDbDataReader dataReader = command.ExecuteReader();
            Connection.Close();

            return dataReader;
        }

        ///


        /// 执行SQL语句返回结果到DataSet中
        ///

        ///
        /// DataSet
        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;
        }

        ///


        /// 执行一查询语句,同时返回查询结果数目
        ///

        ///
        /// sqlResultCount
        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即可。稍后介绍如何使用它,

 

数据库连接字符串还可这样写:

string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Directory.GetCurrentDirectory()+(@"\zhenjiangDBA.mdb");//用于指定数据库文件与.exe文件在同一目录下  

 

C#中如何连接Access2007数据库

 

我们讲的是使用Visual Studio2005来连接Access2007数据库,因为VS2005中没有对Access2007数据库的数据源。
一.首先我们来新建一个Access2007数据库。只需要三个字段ID,姓名,年龄。数据库和表名都起为test(图方便)。
二.新建一个C#的控制台应用程序。
三.导入命名空间System.Data.OleDb
四.将test.accdb放到工程下的bin/debug/下面
五. 在主函数中写如下代码:

 

OleDbConnection dbconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.accdb;Persist Security

Info=False");
            dbconn.Open();
            OleDbCommand cmd = dbconn.CreateCommand();
            cmd.CommandText = "select * from test";
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.Write("{0}:{1}\n", reader["姓名"], reader["年龄"]);
            }

完成,好了按下Ctro+F5看看效果吧!!不过别忘了给数据库添几条数据哦!!!

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template