zoukankan      html  css  js  c++  java
  • .Net(c#) 连接 ACCESS 数据库

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb;
    using System.Data;
    
    namespace DataAccess
    {
        public class DataBasic
        {
            private string cnStr; 
            private OleDbConnection cn;
    
            public DataBasic()
            {
                cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "/????.mdb;Jet OLEDB:Database Password='????'";  
                cn = null;
            }
    
            ~DataBasic()
            {
                try
                {
                    if (cn != null)
                    {
                        cn.Close();
                        cn.Dispose();
                    }
                }
                catch { }
                finally
                {
                    cn.Close();
                    cn.Dispose();
                }
            }
            //打开数据库连接
            protected void Open()
            {
                if (cn == null)
                {
                    cn = new OleDbConnection(cnStr);
                }
                if (cn.State == System.Data.ConnectionState.Closed)
                {
                    cn.Open();
                }
            }
            //关闭数据库连接
            protected void Close()
            {
                if (cn != null) 
                {
                    cn.Close();
                }
            }
            //执行命令操作,以sql字符串为参数传递
            public void Execute(string sql)
            {
                this.Open();
                OleDbCommand cmd = new OleDbCommand(sql,cn);
                cmd.ExecuteNonQuery();
                this.Close();
            }
            //返回数据集
            public DataSet GetDataSet(string sql)
            {
                this.Open();
                OleDbCommand cmd = new OleDbCommand(sql,cn);
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = cmd;
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                this.Close();
                return dataset;
            }
        }
    }


  • 相关阅读:
    程序员修炼之道:从小工到专家有感2
    3月13日
    第一次结对作业(2)
    3月12日
    3月11日
    第一次结对作业
    3月10日
    11月6日
    10月28日
    10月7日
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106927.html
Copyright © 2011-2022 走看看