问题
I am trying to connect to an mdb file from C# 2013 using OdbcConnection. I used to have Office 2000 on my Windows 7 and Windows 8 machines, but I have upgraded to Office 2013 on both and it now longer works on either. My database is still in mdb format.
Here's the code:
const string myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Path\Mydb.mdb";
OdbcConnection connRL = new OdbcConnection();
connRL.Open();
Whatever I have tried I get the following error:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I have Installed the "Microsoft Access Database Engine 2010 Redistributable" and the "2007 Office System Driver: Data Connectivity Components" but neither of them have made any difference. I can create a new Data Source from the VS 2013 Project menu and that all works at design time.
When I create a blank udl file and double click on it and go to the provider tab there's nothing listed about Access or Jet, but I can see the providers in the registry at HKEY_CURRENT_USER\Software\Microsoft\VWDExpress\12.0_Config\DataProviders. Why aren't they there?
What else can I do to get this to work? I have tried everything I can think of.
回答1:
I assume you can't connect to the mdb
through OdbcConnection
. Anyway, to connect to the DB you must specify the connection string on your connection object which is not specified in your code.
To access mdb
use code like this:
const string myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Path\Mydb.mdb";
using(var connRL = new OleDbConnection(myConnectionString))
{
//use your connection
}
来源:https://stackoverflow.com/questions/27876895/connecting-to-an-mdb-file-using-odbcconnection-in-vs2013