Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 697
ميداني حر Avatar asked Dec 24 '12 18:12

ميداني حر


People also ask

How do I connect to Microsoft Access database?

On the Choose your Data Connection page, select New Connection to configure a new data connection. If the data source is not Microsoft Access Database File (OLE DB), select Change to open the Change Data Source dialog box and select Microsoft Access Database File, and then select OK.

Can I use C# in Access?

A developer can work with a Microsoft Access database from Visual C# 2005 or Visual C# . NET by using two separate technologies: Automation and Microsoft ADO.NET. ADO.NET is the preferred technology if you want to work with data objects, such as tables and queries in an Access database.

How do I open an existing Access database?

Open an existing Access databaseOn the File tab, click Open. In the Open dialog box, browse to the database that you want to open. Do one of the following: Double-click the database to open it in the default mode specified in the Access Options dialog box or the mode that was set by an administrative policy.

How do I connect to an Access database in SQL?

Open the destination Access database. On the External Data tab, click ODBC Database. Click Link to the data source by creating a linked table > OK and follow the steps in the wizard.In the Select Data Source box, if the . dsn file you want to use already exists, click the file in the list.


1 Answers

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 :-)

like image 67
Steve Avatar answered Oct 05 '22 03:10

Steve