• 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;";
                }
            }

    }

  • 相关阅读:
    ASP.net实现WEB站点的后台定时任务[转]
    個人所得稅計算
    當VS2005 遇上 LINQ[转]
    NBearV3中文教程总目录
    C#开源框架
    excel 不能使用对象链接和嵌入的错误
    PetShop 学习
    ADHelper类与扩展应用
    (javascript,treeview)treeview通过checkbox来进行全选单选
    (javascript)动态添加的控件如何设置其属性
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306858.html
Copyright © 2020-2023  润新知