04cDatabase class

xiaoxiao2021-03-06  60

CDATABASE class

To establish a connection with a data source, you should first construct a CDATABASE object, then call the cdatabase's Open member function. The Open function is responsible for establishing a connection, its declaration is

virtual BOOL Open (LPCTSTR lpszDSN, BOOL bExclusive = FALSE, BOOL bReadOnly = FALSE, LPCTSTR lpszConnect = "ODBC;", BOOL bUseCursorLib = TRUE); throw (CDBException, CMemoryException);

The parameter LPSZDSN specifies the data source name (the method of constructing the data source will be described later), and the data source name 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, the LPSZDSN is NULL, a data source dialog is displayed, and the user can select a data source in this dialog. Parameter bexClusive Description Do you exclusive data source, because the current version of the class library does not support exclusive way, the value of this parameter should be False, which means that the data source is shared. Parameters BreadOnly If true, the connection to the data source is read-only. The parameter LPSZConnect specifies a connection string, and the connection string may include information such as data source name, user account (ID), and passwords, and "ODBC" in the string denotes to be connected to an ODBC data source. If the parameter busecursorlib is loaded, the optical b library will be loaded, otherwise it is not loaded, the snapshot requires the optical label, the dynamic set does not require the optical label. If the connection is successful, the function returns TRUE. If false returns, the user will press the Cancel button in the Data Source dialog box. If there is an error inside the function, the framework generates an exception.

Here is some examples of calling the Open function.

CDATABASE M_DB; / / Embed a CDatabase object in the document class

/ / Connect to a data source called "Student Registration"

m_db.open ("student registration");

/ / Specify the user account and password while connecting the data source

M_DB.Open (NULL, FALSE, FALSE, "ODBC; DSN = Student Registration; UID = ZYF; PWD = 1234");

m_db.open (null); // will pop up a data source dialog

To detach from a data source, you can call the function close. After disengage, the Open function can be called again to create a new connection. Calling isopen can determine if there is a connection, calling getConnect to return the current connection string. Function declaration

Virtual void close ();

Bool isopen () const; // Returns True indicates that there is a connection

CONST CSTRING & GETCONNECT () Const;

CDATABASE's destructor calls Close, so as long as the CDATABASE object is deleted, you can detach from the data source.

转载请注明原文地址:https://www.9cbs.com/read-84776.html

New Post(0)