March 2008
Intermediate to advanced
982 pages
22h 43m
English
You want to connect to a Microsoft Access database that has a database password.
Use the Jet OLEDB:Database Password attribute in the connection string to specify the password.
The solution creates and opens a connection to a password-secured Microsoft Access database using the OLE DB .NET data provider. Information about the connection is displayed from the properties of the OleDbConnection object.
The C# code in Program.cs in the project ConnectPasswordAccessDatabase is shown in Example 1-12.
Example 1-12. File: Program.cs for ConnectPasswordAccessDatabase 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;" +
"Jet OLEDB:Database Password=password;";
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-11.
Figure 1-11. Output for ConnectPasswordAccessDatabase solution ...
Read now
Unlock full access