OdbcConnection、OleDbConnection均可读取mdb文件
OleDbConnection
private static void UseOleDbConnection(string filePath,string sql) { var myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + $"Data Source={filePath};" + "Persist Security Info=True;" + "Jet OLEDB:Database Password=myPassword;"; using (OleDbConnection myConnection = new OleDbConnection()) { // 打开OleDb连接 myConnection.ConnectionString = myConnectionString; myConnection.Open(); // 执行查询 OleDbCommand cmd = myConnection.CreateCommand(); cmd.CommandText = sql; OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); // 完成查询操作后关闭连接 // 加载数据到DataTable DataTable myDataTable = new DataTable(); myDataTable.Load(reader); } }
OdbcConnection
private static void UseOdbcConnection(string filePath, string sql) { var myConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};" + $"Dbq={filePath};Uid=Admin;Pwd=;"; using (OdbcConnection myConnection = new OdbcConnection()) { // 打开OleDb连接 myConnection.ConnectionString = myConnectionString; myConnection.Open(); // 执行查询 OdbcDataAdapter pAdapter = new OdbcDataAdapter(sql, myConnection); DataTable dataTable = new DataTable(); pAdapter.Fill(dataTable); } }
ODBC & Ole Db
OleDbConnection类的对象连接支持OLE DB的数据库,如Access;而OdbcConnection类的对象连接任何支持ODBC的数据库
二者的区别和联系:ODBC、OLEDB和ADO之间的关系 ,以及性能比较
示例代码
ReadMdbFileDemo