Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

c# 4.0 - How to open connection with Microsoft Access database in C#

I'm using Microsoft Access to create my database. Here's my code:

static string Constr = "Provider=Microsoft.Jet.OLEDB.4.0;" 
                     + "Data Source = MyData.accdb";
OleDbConnection Conn = new OleDbConnection(Constr);
DataSet DataSet1 = new DataSet();
string SQLstr = "Select * from Tabel";
OleDbDataAdapter DataAdapter1;
Conn.Open();

I'm getting this exception:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Unrecognized database format

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to open an Access database created with Access 2007/2010 (accdb) with an OleDb provider that can understand only MDB files created with Access 2003

The provider to use is Microsoft.ACE.OleDB.12.0 that should be already installed with the latest version of Office.

In alternative you could download the appropriate bits from the Microsoft Access Database Engine
Just pay attention to download the version appropriate for the platform (x86 or x64) you use.

And that's the connection string to use

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MyData.accdb";

NOTE: Keep in mind that if you have Office installed, the corresponding provider has the same bitness of Office, so with Office 64bit comes the ACE in 64bit version and the same for 32bit. This scenario poses a very difficult problem in deployment of your application. You need to have you application compiled with the same bitness or you will not be able to use the provider installed. (Or disinstall the incompatible Office version and reinstall the compatible one. Of course if you should be able to persuade your customer that Office is the incompatible app :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...