using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Linq; using System.Text; namespace UAS_MES.DataOperate { class AccessDBHelper { private string _fileName; private string _connectionString; private OleDbConnection _odcConnection; public AccessDBHelper(string fileName) { this._fileName = fileName; this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";"; } public void Open() { try { // 建立连接 this._odcConnection = new OleDbConnection(this._connectionString); // 打开连接 this._odcConnection.Open(); } catch (Exception) { throw new Exception("嘗試打开 " + this._fileName + " 失敗, 請確認文件是否存在!"); } } public void Close() { this._odcConnection.Close(); } public DataSet GetDataSet(string sql) { DataSet ds = new DataSet(); try { OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection); adapter.Fill(ds); } catch (Exception) { throw new Exception("sql語句: " + sql + " 執行失敗!"); } return ds; } } }