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

    }

  • 相关阅读:
    Jsp的内置标签和jstl标签
    Jsp中的EL表达式
    JSP和servlet结合案例
    Jsp:内置对象和四种域对象的理解
    编码实战Web端联系人的增删改查
    Session案例
    Cookie案例分析
    会话数据的保存——cookie
    ServletContext和ServletConfig
    Servlet(1)
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306858.html
Copyright © 2011-2022 走看看