/// <summary>
    /// Access数据库操作
    /// </summary>
    public class AccessDB
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        private string connString;
        /// <summary>
        /// 连接对象
        /// </summary>
        private OleDbConnection conn;

        private string commandText;
        /// <summary>
        /// 执行的语句
        /// </summary>
        public string CommandText
        {
            get { return commandText; }
            set { commandText = value; }
        }

        public AccessDB()
        {
            try
            {
                connString = ConfigurationManager.AppSettings[“AccessConnString”].ToString();

                string[] tempConnStrings = connString.Split(\’#\’);
                tempConnStrings[1] = AppDomain.CurrentDomain.BaseDirectory + tempConnStrings[1];
                connString = tempConnStrings[0] + tempConnStrings[1] + tempConnStrings[2];

            }
            catch (Exception ex)
            {
                PublicTool.WriteLog(“在类” + this.ToString() + “的构造方法中发声异常!\r\n” + ex.ToString());
            }
        }

        /// <summary>
        /// 得到数据库连接
        /// </summary>
        /// <returns></returns>
        public OleDbConnection GetConnection()
        {
            conn = new OleDbConnection(connString);
            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                PublicTool.WriteLog(“在类” + this.ToString() + “的方法GetConnection()中发声异常!\r\n” + e.ToString());
            }
            return conn;
        }

        /// <summary>
        /// 得到命令对象
        /// </summary>
        public OleDbCommand GetOleDbCommand()
        {
            OleDbConnection conn = this.GetConnection();
            OleDbCommand cmd = new OleDbCommand(commandText, conn);
            return cmd;
        }

        /// <summary>
        /// 得到数据读取器
        /// </summary>
        /// <returns></returns>
        public OleDbDataReader GetOleDbDataReader()
        {
            OleDbCommand cmd = this.GetOleDbCommand();
            OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConnection()
        {
            conn.Close();
        }
    }

版权声明:本文为wangzb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wangzb/archive/2010/02/05/1664429.html