To establish a connection with the data source, you must first construct a cdatabase object and then call the open
The member function. OPEN function is responsible for establishing a connection, and its declaration is
Virtual bool open
(
Lpctstr lpszdsn,
Bool bexclusive = false,
Bool breadonly = false,
Lpctstr lpszconnect = "ODBC ;",
Bool busecursorlib = true
);
Throw (cdbexception, cmemoryexception );
Parameter description:
Lpszdsn specifies the data source name, which can also be included in the lpszconnect parameter. At this time, lpszdsn must be null. If the data source name is not provided in the function and lpszdsn is null, a Data Source dialog box is displayed. You can select a data source in the dialog box.
Bexclusive indicates whether the data source is exclusive. Because the current version of the class library does not support exclusive mode, the value of this parameter should be false, which indicates that the data source is shared.
If breadonly is true, the connection to the data source is read-only.
Lpszconnect specifies a connection string that contains information such as the data source name, User Account (ID), and password. "ODBC" in the string indicates that you want to connect to an ODBC data source.
If the value of busecursorlib is true, the cursor library is loaded; otherwise, the cursor library is not required for snapshots, And the cursor library is not required for dynamic sets.
Reference code:
# Include "afxdb. H"
Cdatabase m_db;
M_db.open (null, false, false, _ T ("ODBC; uid = sa; Pwd = sa"), true); // connect to the database
To remove from a data source, you can call the close function. After the disconnection, you can call the OPEN function again to establish a new connection. Call isopen to determine whether a connection exists. Call getconnect to return the current connection string.