08 DAO

xiaoxiao2021-03-06  64

DAO

10.8.1 What is DAO

DAO (Database Access Object) uses the Microsoft Jet Database Engine to access the database. Microsoft Jet provides a data engine with products such as Access and Visual Basic.

Like ODBC, DAO provides a set of API for programming. The MFC also provides a set of DAO classes that encapsulate the underlying API, which greatly simplifies the development of procedures. With the MFC's DAO class, users can write applications independent of DBMS.

DAO is introduced from Visual C 4.0. Generally speaking, the DAO class provides a broader support than ODBC classes. On the one hand, as long as there is an ODBC driver, use the Microsoft Jet DAO to access the ODBC data source. On the other hand, since the DAO is based on the Microsoft Jet engine, it has good performance when accessing the Access database (ie * .mdb file).

10.8.2 Similarity of DAO and ODBC

The DAO class has a lot of similarities compared to the ODBC class, which mainly has the following:

Both support access to various ODBC data sources. Although the data engine used by the two is different, it can meet the requirements of users who write independent of DBMS applications. DAO provides an MFC class similar to ODBC. For example, DAO's CDAODatabase class corresponds to the CDATABASE class of ODBC, CDAORECORDSET corresponds to CRecordView, CDAOException, corresponding CDBEXCeption. These corresponding class functions are similar, and most of their member functions are the same. AppWizard and ClassWizard provide similar support to applications that use DAO and ODBC objects.

Due to many aspects of DAO and ODBC classes, it is easy to learn to use DAO as long as the user has mastered ODBC. In fact, users can easily transplant the database application from ODBC to DAO.

Visual C provides an example called DaoEnrol, which is actually a DAO version of Enroll. The reader can open the DAOENROL project to see, and its source code is extremely similar to Enroll. The reader can establish DAOENROL according to the steps to establish Enroll, only several places have a difference, which mainly has the following points:

The selected data source is different. When you create DaOenrol with AppWizard, and when you create a CDAORECORDSET class with ClassWizard, you should choose DAO instead of ODBC in the Database Options dialog. Moreover, DAO's data source is specified by selecting a .mdb file, ie, click "..." button, select the .mdb file you want to access in the File dialog box. The default type of record set is different. The default type of the ODBC record is a snapshot, while DAO is a dynamic set (DYNASET). Parameterized approach. The parameter in the m_strfilter and m_strsort of the DAO record is not "?", But a meaningful parameter name. For example, there is a parameter called CourseIdParam in the filter below. m_pSet-> m_strFilter = "CourseID = CourseIDParam"; in DoFieldExchange function, has the following two rows: pFX-> SetFieldType (CDaoFieldExchange :: param); DFX first function; DFX_Text (pFX, _T ( "CourseIDParam"), m_strCourseIDParam) Two parameters are also CourseidParam. Different ways to deal with exceptions. For example, when deleting records, the abnormality is processed as follows: try {m_pset-> delete ();} catch (cdaoException * e) {afxMessageBox (e-> m_perrorinfo-> m_strdescription); E-> delete (); }

In addition to the above differences, AppWizard and ClassWizard have also hidden some subtle differences. For example, the DAO recordset is used as a DAO Record Field Exchange instead of RFX, which is used in DOFIELDEXCHANGE in the DAO record. DFX functions are not RFX functions.

10.8.3 DAO features

DAO can access the ODBC data source via the ODBC driver. But DAO is based on Microsoft Jet Engine, through the engine, DAO can directly access databases such as Access, FoxPro, DBASE, Paradox, Excel, and Lotus WK. The CDAODatabase class can be connected directly to these databases without having to register DSNs in the ODBC Manager. For example, the following code is used to open a FoxPro database:

Cdaodatabase daodb;

DAODB.Open ("", False, False, "FoxPro 2.5; Database = C: // Zyf");

CDAODATABASE :: Open function is used to connect a database, the declaration of the function is:

Virtual Void Open (LPCTSTSTR LPSZNAME, BOOL BEXCLUSIVE = FALSE, BOOL BREADONLY = false, lpctstr lpszconnect = _t (")); throw (cdaoException, cmemoryexception);

Parameters BexClusive If true, the function opens the database in exclusive way, otherwise you will use the sharing method. If BREADOONLY is TRUE, then open the database in a read-only mode. If you want to open an Access database, you can specify the MDB file name in the lpszName parameter. If you want to access a non-Access database, you should make this parameter "", and a connection string is explained in LPSZConnect. The form of the connection string is generally "database type; Database = path (file)", such as "DBASE III; Database = C: // mydir" Open function can also open an ODBC data source, but this requires the corresponding ODBC driver And need to register DSN in the ODBC Manager. At this time, LPSZConnect is "ODBC; DSN = MyDataSource". Obviously, use DAO to access a database like FoxPro, directly open the more than to save it as an ODBC data source.

Supporting DDL is an important embodiment of DAO's good support for database programming. DDL (Data Definition Language) is called "Data Definition Language" in SQL Term, which is used to complete the operation of generating, modify, and delete database structures. The ODBC class only supports DML (Data Manipulation Language, Data Operation Language), which does not support DDL, so you can only complete the data of the data with the ODBC class, and the structure of the database cannot be involved. To perform DDL operation, only through the ODBC API. The DAO class also provides support for DML and DDL, which means that programs can use the DAO class to easily create databases and modify the structure of the database.

Compared to ODBC, DAO provides some new classes to enhance its functions, including:

The CDaotableDef class provides the definition of the structure of the table. Call CDAOTABLEDEF :: Open can get the structure definition of the table. Call cdaotabledef :: Create can create a new table, call cdaotabledef :: CreateField to add a field to the table, call CDaOTablesDef :: CreateIndex to add an index to the table. Call CDAOTABEDEF :: Append can save the newly created table into the database.

The CDAOQueryDef class represents a query definition, which can be stored in the database.

CDaoWorkSpace provides a data work area (Workspace). A work area can include several databases, and the workspace can perform all or separate transactions to the database, and the workspace is also responsible for the security of the database. If necessary, the program can open multiple workspaces.

Another important feature of DAO is that it provides powerful support for the Access database. Since DAO is based on Microsoft Jet Engine, DAO must do more articles on the Access database. For example, call CDAODatabase :: CREATE to create a MDB file directly, the code as follows: m_db.create ("c: //mydirl/Mydb.mdb);

With AppWizard and ClassWizard, users can easily develop DAO-based Access database applications with excellent performance.

10.8.4 ODBC or DAO

Since DAO can access the ODBC data source, the following can be replaced by the DAO replacement ODBC:

Better performance can be obtained in some cases, especially when accessing the Microsoft Jet (.mdb) database. Compatible with ODBC compatible DAO allows data effective check DAO allows users to explain the relationship between tables and tables

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

New Post(0)