Using Database Passwords with the MFC DAO ClassesWith a database that has a database password, the password is stored in the database (.mdb) file itself, not in a separate file.
The following code shows how to use MFC DAO to open a database that has a database password:
//myDB is a valid CDaoDatabase object.
Using Database Passwords with the MFC ODBC ClassesThe following example shows how to use MFC ODBC to open a database that has a database password. This code is from a CRecordset-derived class that was generated with the MFC AppWizard.
Using User-Level Passwords with the MFC DAO ClassesThe process is more complex if you are using the MFC DAO classes, and are connecting to a database that has user-level security. First, you need to set the SystemDB property of the DBEngine object to point to the appropriate workgroup information file. This must be done before any DAO objects have been initialized; otherwise, the setting has no effect. The code below is in the InitInstance function of a DAO MFC AppWizard application. Note that extra code is shown before and after the DAO code in order to illustrate where the DAO code would go in a typical InitInstance implementation:
NOTE: Visual C++ .NET does not have DAO MFC AppWizard. For more information, see MFC Technical Note 54 (TN054) in the MSDN library.
// Parse command line for standard shell commands, DDE, file open.
// Inserted code does 2 things: Sets the version of DAO to use, and //
// sets the System.mdw file to the one that we want. //
// Use DAO 3.6.
AfxGetModuleState()->m_dwVersion = 0x0601;
//Set the system DB to the one we want.
//May need to call AfxDaoTerm if DAO is already running
CString strSystemMDB = "f:\\secured.mdw";
COleVariant varSystemDB( strSystemMDB, VT_BSTRT );
// Initialize DAO for MFC.
DAODBEngine* pDBEngine = AfxDaoGetEngine( );
ASSERT( pDBEngine != NULL );
// Call put_SystemDB method to set the
// system database for DAO engine.
DAO_CHECK( pDBEngine->put_SystemDB( varSystemDB.bstrVal ) );
// Dispatch commands specified on the command line.
// The one and only window has been initialized, so show and update it.
m_pSet = &GetDocument()->m_securedRSSet;
// In order to open a secured Access DB we must have a Workspace
// that has been opened with the correct user name and password.
// Note that the system DB is not set here, because it was set in
myWS.Create("New", "Lamont", "Lamont");
Using User-Level Passwords with the MFC ODBC ClassesWhen connecting to the database through ODBC, it is much easier to specify which workgroup information file you want to use. Simply pass a parameter in the connection string that points to the workgroup information file to use. The code below is from a CRecordset-derived class that was generated with the MFC AppWizard.
Artikelnummer: 270663 – Letzte Überarbeitung: 11.01.2006 – Revision: 1