March 2008
Intermediate to advanced
982 pages
22h 43m
English
You want to connect to a Microsoft Access database.
Use the OLE DB .NET data provider.
The solution creates and opens a connection to a Microsoft Access database using the OLE DB .NET data provider. Information about the connection is displayed.
The C# code in Program.cs in the project ConnectAccessDatabase is shown in Example 1-11.
Example 1-11. File: Program.cs for ConnectAccessDatabase solution
using System;
using System.Data;
using System.Data.OleDb;
namespace ConnectAccessDatabase
{
class Program
{
static void Main(string[] args)
{
string oledbConnectString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
@"C:\Documents and Settings\bill\My Documents\" +
"Northwind 2007.accdb;";
using (OleDbConnection connection =
new OleDbConnection(oledbConnectString))
{
connection.Open( );
// Output some connection and database information.
Console.WriteLine("Connection State: {0}", connection.State);
Console.WriteLine("OLE DB Provider: {0}", connection.Provider);
Console.WriteLine("Server Version: {0}", connection.ServerVersion);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey( );
}
}
}The output is shown in Figure 1-10.

Figure 1-10. Output for ConnectAccessDatabase solution
You can connect to a Microsoft Access database using the OLE DB .NET data provider. The OLE DB connection uses the Microsoft.ACE.OLEDB.12.0, ...
Read now
Unlock full access