Often someone asked how to use ADO to open an Access database with password, let me know in detail to use the UDL file to open an Access database with password.
Suppose the Access database file client.mdb opens the password is "cxw".
Step 1: Create a UDL file
Create a text file Test.txt under Win2000, and then change the file suffix to .UDL. Double-click the file to open Test.udl The following dialog box:
figure 1
Select the driver of the database in the above dialog, we select the drive Microsoft Jet 4.0 Ole DB Provider of Access2000, click "Next>" to enter the dialog box shown below:
figure 2
First select the data source file, click the "..." button to select the Access database file. Enter the username admin (the user name of the Access database is fixed) in the UserName Editing box. Select "Blank Password". Others maintain the default value, enter the "all" page as shown below:
image 3
Select "Jet OLE DB: DATABASE PASSWORD" in the above image, click "Edit Value ..." button to enter the password "CXW" of Access Database Client.mdb, point to determine exit. Select "Persist Security Info" in the dialog box above, click "Edit Value ..." to select False. Others maintain the default value. Thus, the UDL file has been set, we can enter the "Connection" page (as shown in Figure 2), click "Test Connection" in the dialog to test whether the connection is successful, the test success shows that the UDL file has been successfully established.
Step 2, establish an ADO connection
First join in the stdafx.h file:
#pragma Warning (Disable: 4146)
#import "c: / program files / compon files / system / ado / msado15.dll" /
NO_NAMESPACE RENAME ("EOF", "Endoffile" #pragma Warning (Default: 4146)
And also pay attention to the location of this paragraph, back of all the include statements in stdafx.h
Since ADO is a COM component, the COM environment is initialized in the beginning of the application:
:: Coinitialize (NULL);
Then add the following code when you need to connect the database:
_ConnectionPTR m_pdbconn;
m_pdbconn.createInstance (__ uuidof (connection)); // Create a COM object
m_pdbconn-> connectionstring = "file name = c: //mydir/test.ud";
Try {
m_pdbconn-> open (",", ",", null);
} catch (_COM_ERROR & E) {
// Treat an exception code
.........................
m_pdbconn = NULL;
} This has already established an ADO connection using the UDL file. The advantage of connecting the database using the UDL file is: When you change the database name or username password, you only need to change the UDL file, do not need to modify you The source program, of course, no need to recompile.
Recently I saw a netizen comment on using UDL files with UDL files, and verified. So you can solve the following method: m_pdbconn-> open ("provider = microsoft.jet.Oledb.4.0; data source = client.mdb; persist security info = false; Jet OLEDB: Database Password = CXW", ", "", admodeunknown; you can send an email discussion
Peckermen@163.com