C# 应用程序类中定义ACCESS数据库文件地址的方法

C# 应用程序类中定义ACCESS数据库文件地址的方法
方法一:
数据库访问类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace Link.DataBase
{
    public class DBManage
    {
        System.Data.OleDb.OleDbConnection gConn = null;

        public OleDbConnection Conn
        {
            get { return gConn; }
        }
        //定义类公开对外的数据库连接对象变量
        static string strConnectionString;
        //设置数据链接参数
        static public string ConnectionString
        {
            set
            {
                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + value + ";User ID=admin;Password=;Jet OLEDB:Database Password=";
            }
        }

        //数据库连接对象
        public DBManage()
        {           
            gConn = new OleDbConnection(strConnectionString);
        }
        //查询数据库资料
        public OleDbDataReader ExecuteReader(string SQL)
        {
            OleDbDataReader reValue = null;
            OleDbCommand comm = new OleDbCommand();
            comm.CommandText = SQL;
            comm.Connection = gConn;
            reValue = comm.ExecuteReader();
            return reValue;
        }
        //执行SQL查询语句返回数据表对象
        public DataTable ExecuteTable(string SQL)
        {
            DataTable table = new DataTable();
            gConn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.CommandText = SQL;
            comm.Connection = gConn;
            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(comm);
            System.Data.OleDb.OleDbCommandBuilder commBuild = new System.Data.OleDb.OleDbCommandBuilder(adapter);
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            adapter.Fill(table);
            gConn.Close();
            return table;
        }
        //直接运行输入SQL语句参数
        public void ExecuteNonQuery(string SQL)
        {           
            OleDbCommand comm = new OleDbCommand();
            gConn.Open();
            comm.CommandText = SQL;
            comm.Connection = gConn;
            comm.ExecuteNonQuery();
            gConn.Close();
        }

    }
}
窗体调用方法

 public LinksManagement()
        {
            DBManage.ConnectionString = Application.StartupPath + @"/ExchangeLinksDB.mdb";
            InitializeComponent();
        }

方法二:

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

//数据访问层
namespace Link.DataBase
{
    public class CreateUrlManager
    {

        private OleDbConnection cnn;
      
        private static string strConnectionString;
        
      public CreateUrlManager()
        {
            if (System.Environment.CurrentDirectory == AppDomain.CurrentDomain.BaseDirectory)//Windows应用程序则相等
            {
                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Bin//ExchangeLinksDB.mdb;";
            }
            else//Windows类程序不相等
            {
                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "ExchangeLinksDB.mdb;";
            }
        }

}

posted on 2008-05-14 12:10  风灵溪清  阅读(166)  评论(0编辑  收藏  举报

导航