Opens a specified database in a CdbWorkspace object and returns a reference to the CdbDatabase object that represents it.
Syntax
CdbDatabaseOpenDatabase(LPCTSTR pstrName,
LONG IOptions = -1L,
BOOL bReadOnly = FALSE,
LPCTSTR pstrConnect = NULL);
Parameters
Type | Argument | Description |
LPCTSTR | pstrName | A pointer to a string that is the name of an existing Microsoft Jet database file or the data source name (DSN) of an ODBC data source. See the Name property for more information about setting this value. |
LONG | lOptions | Optional. A Long value that specifies how the ODBC driver manager should prompt the user for connection information such as DSN, user name, and password. Possible values are described in Remarks. |
BOOL | bReadOnly | Optional.
TRUEOpens the database with read-only access. FALSE(Default) Opens the database with read/write access. |
LPCTSTR | pstrConnect | Optional. A pointer to a string composed of a database type specifier and zero or more parameters separated by semicolons. This parameter passes additional information to ODBC and certain ISAM drivers as needed. |
Settings
For ODBCDirect workspaces, the IOptions argument determines if and when to prompt the user to establish the connection. You can use one of the following constants.
Constant | Description |
dbDriverNoPrompt | The ODBC Driver Manager uses the connection string provided in pstrName and pstrConnect. If you don't provide sufficient information, a run-time error occurs. |
dbDriverPrompt | The ODBC Driver Manager displays the ODBC Data Sources dialog box, which displays any relevant information supplied in pstrName or pstrConnect. The connection string is made up of the DSN that the user selects via the dialog boxes, or, if the user doesn't specify a DSN, the default DSN is used. |
dbDriverComplete | (Default) If the pstrConnect and pstrName arguments include all the necessary information to complete a connection, the ODBC Driver Manager uses the string in connect. Otherwise it behaves as it does when you specify dbDriverPrompt. |
dbDriverCompleteRequired | This option behaves like dbDriverComplete except the ODBC driver disables the prompts for any information not required to complete the connection. |
Remarks
When you open a database, it is automatically added to the Databases collection. Further, in an ODBCDirect workspace, the CdbConnection object corresponding to the new CdbDatabase object is also created and appended to the Connections collection of the same CdbWorkspace object.
Some considerations apply when you use pstrName:
"ODBC;"
, a dialog box listing all registered ODBC data source names is displayed so the user can select a database.The pstrConnect argument is expressed in two parts: the database type, followed by a semicolon (;) and the optional arguments. You must first provide the database type, such as "ODBC;"
or "FoxPro 2.5;"
. The optional arguments follow in no particular order, separated by semicolons. One of the parameters may be the password (if one is assigned). For example:
"FoxPro 2.5; pwd=mypassword"
Using the NewPassword method on a CdbDatabase object other than an ODBCDirect database changes the password parameter that appears in the ";pwd=..."
part of this argument. You must supply the lOptions and bReadOnly arguments to supply a source string. See the Connect property for syntax.
To close a database, and thus remove the CdbDatabase object from the Databases collection, use the Close method on the object.
Note When you access a Microsoft Jet-connected ODBC data source, you can improve your application's performance by opening a CdbDatabase object connected to the ODBC data source, rather than by linking individual CdbTableDef objects to specific tables in the ODBC data source.
Usage
#include <afxole.h>
#include <dbdao.h>
CdbDBEngine dben;
CdbDatabase db;
// Open a local Jet database in exclusive, read/write mode.
db = dben.OpenDatabase(_T("C:\\YourJetDB.mdb"), TRUE);