zoukankan      html  css  js  c++  java
  • 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;";
                }
            }

    }

  • 相关阅读:
    浏览器同源政策及其规避方法---转阮大神
    js跨域详解
    js中top、self、parent
    杂记
    DOM 踩踩踩
    java idea 连接数据库
    数据库mySQL常用命令
    用迭代实现80人围成一圈逢3取出
    如何把通过类建立的对象存入数组中.
    面向对象编程
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306858.html
Copyright © 2011-2022 走看看