Connecting to a Password-Protected Microsoft Access

Posted on 2009-09-24 15:17  Metisria Geo  阅读(433)  评论(0)    收藏  举报

Recipe 1.2. Connecting to a Password-Protected Microsoft Access

1.2.1. Problem

You want to connect to a Microsoft Access database that has a database password.

1.2.2. Solution

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-2.

Example 1-2. 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:\Users\Chinekm\Documents\" +
                "Northwind.accdb;" +
                "Jet OLEDB:Database Password=password;";

            using (OleDbConnection connection = new OleDbConnection(oledbConnectString))
            {
                connection.Open(  );
                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(  );
        }
    }
}

1.2.3. Discussion

A Microsoft Access database password requires that users enter a password to obtain access to the database and database objects. This is also known as share-level security. A password does not allow groups or users to have distinct levels of access or permissions. Anyone with the password has unrestricted access to the database.

The Set Database command from the Tools -> Security menu is used to set up a database password.

The OLE DB provider for the Microsoft Access database engine has several provider-specific connection string attributes in addition to those defined by ADO.NET. To open a database secured by a Microsoft Access database password, use the Jet OLEDB:Database Password attribute in the connection string to specify the password. This corresponds to the OLE DB property DBPROP_JETOLEDB_DATABASEPASSWORD.

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3