Connecting to Access 2007 databases from C#

Posted: (EET/GMT+2)

 

I did some quick tests today regarding connecting to Access 2007 databases from C# with ADO.NET.

As you are surely aware, accessing older Access database files (.MDB file extension) is really simple from .NET: you just connect to the database, and all the drivers are already there in the operating system. However, when you move to Access 2007, the file extension changes to .ACCDB, and you cannot access these databases without Access 2007 installed on the same machine. If you try that, you will get the error message "Unrecognized database format 'Northiwind.accdb'."

The connection strings look like this for Access 2000-2003 databases:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Northiwind.mdb"

And for Access 2007 databases they look like this:

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

But once you have Access 2007 installed (or rather, the correct drivers), using those databases with C#'s OleDbConnection/OleDbCommand works just like before.