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

    }

  • 相关阅读:
    PAT1066(AVL树)
    判断是对象,还是数组对象
    electron打包成.exe后限制只启动一个应用
    electron启动出现短暂的白屏
    js sort方法根据数组中对象的某一个属性值进行排序
    VUE DIV模拟input框的基本处理
    vue 设置 input 为不可以编辑
    VUE实现限制输入框最多输入15个中文,或者30个英文
    electron 点击事件无效
    electron监听系统托盘,electron是否最小化到系统托盘
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306858.html
Copyright © 2020-2023  润新知