Access数据库操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

 

 public class AccessDB
    {
        private static string connString;

        public static string ConnString
        {
            get { return AccessDB.connString; }
            set { AccessDB.connString = value; }
        }
        private static OleDbConnection conn;
        private string commandText;

        public string CommandText
        {
            get { return commandText; }
            set { commandText = value; }
        }

        public AccessDB()
        {
            connString = ConfigurationManager.ConnectionStrings["AccessConnString"].ToString();

            string[] tempConnStrings = connString.Split('#');
            tempConnStrings[1] = AppDomain.CurrentDomain.BaseDirectory + tempConnStrings[1];
            connString = tempConnStrings[0] + tempConnStrings[1] + tempConnStrings[2];
            //connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + @"\Kangzhi.Sedu.Material.dll;User Id=admin;Password=;";
        }
        /// <summary>
        /// 得到数据库连接
        /// </summary>
        /// <returns></returns>
        public static OleDbConnection GetConnection()
        {
            conn = new OleDbConnection(ConnString);
            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            return conn;
        }
        /// <summary>
        /// 得到命令对象
        /// </summary>
        /// <returns></returns>
        public OleDbCommand GetOleDbCommmand()
        {
            OleDbConnection conn = AccessDB.GetConnection();
            OleDbCommand cmd = new OleDbCommand(commandText, conn);
            return cmd;
        }
        /// <summary>
        /// 得到数据读取器
        /// </summary>
        /// <returns></returns>
        public OleDbDataReader GetOleDbDataReader()
        {
            OleDbCommand cmd = this.GetOleDbCommmand();
            OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConnection()
        {
            conn.Close();
        }
    }

你可能感兴趣的:(Access)