c#讀取mdb

            this._connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";//數據庫07以前版本
            this._connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";//數據庫07以後版本

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

namespace test
{
    public class MdbHelp
    {
        private string _fileName; 
        private string _connectionString; 
        private OleDbConnection _odcConnection; 
 
        /// <summary> 
        /// 構建函數 
        /// </summary> 
        /// <param name="fileName">MDB文件夾的位置</param> 
        public MdbHelp(string fileName) 
        { 
            this._fileName = fileName; 
            //this._connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";//Access 07以前版本
            this._connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";//Access 07以後版本
        } 
        /// <summary> 
        /// 打開數據庫
        /// </summary> 
        public void Open() 
        {
            try
            {
                this._odcConnection = new OleDbConnection(this._connectionString);
                this._odcConnection.Open();
            }
            catch (Exception)
            {
                throw new Exception("嘗試打開" + this._fileName + "失敗,請確認該日期是否是工作日!");
            }
        }
        /// <summary> 
        /// 斷開鏈接(關閉數據庫文件)
        /// </summary> 
        public void Close() 
        { 
            this._odcConnection.Close(); 
        } 

        /// <summary> 
        /// 根據SQL命令返回一個DataTable
        /// </summary> 
        /// <param name="sql">sql/param> 
        /// <returns>DataTable</returns> 
        public DataTable GetDataTable(string sql) 
        {
            DataTable dt = new DataTable(); 
            try 
            { 
                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection); 
                adapter.Fill(dt); 
            } 
            catch (Exception) 
            { 
                throw new Exception("sql語句:" + sql + " 執行失敗!"); 
            } 
            return dt; 
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章