操作mdb文件的帮助类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.OleDb; namespace GBQ.Lidar.AMS.Utils { public class MDBHelper { private string _fileName; private string _connectionString; private OleDbConnection _odcConnection; /// <summary> /// 构建函数 /// </summary> /// <param name="fileName">MDB文件夹的位置</param> public MDBHelper(string fileName) { this._fileName = fileName; this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";"; } /// <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命令返回一个dataSet /// </summary> /// <param name="sql">sql/param> /// <returns>DataTable</returns> public DataTable GetDataSet(string sql) { DataTable ds = new DataTable(); try { OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection); adapter.Fill(ds); } catch (Exception) { throw new Exception("sql语句:" + sql + " 执行失败!"); } return ds; } } }
直接调用即可