DAO (Database access Object) uses the Microsoft Jet database engine to access the database.
Microsoft Jet provides a data engine for products such as access and Visual Basic.
Like ODBC, DAO provides a set of APIs for programmatic use. MFC also provides a set of DAO classes that encapsulate the
The underlying API, which greatly simplifies the development of the program. Using MFC's DAO classes, users can write independent
The application of the DBMS.
DAO was introduced from the visual c++4.0 version. Generally speaking, the DAO class provides a wider range than the ODBC class
Pan-Support. On the one hand, if you have an ODBC driver, you can use Microsoft Jet's DAO to access
ODBC data source. On the other hand, because DAO is based on the Microsoft Jet engine, it accesses
Access database (that is, *. MDB file), it has good performance.
The similarities between 10.8.2 DAO and ODBC
There are a number of similarities between DAO classes and ODBC classes, which are mainly as follows:
Both support access to a variety of ODBC data sources. Although the data engines used are different, they can be full
Sufficient users to write requirements for applications that are independent of the DBMS.
DAO provides MFC classes that are similar to ODBC features. For example, DAO's CDaoDatabase class corresponds to the ODBC
CDatabase class, CDaoRecordset corresponds to Crecordset,cdaorecordview corresponding CRecordView
, cdaoexception corresponds to CDBException. These corresponding classes function similarly, and most of their members
Functions are the same.
AppWizard and ClassWizard provide similar support for applications that use DAO and ODBC objects.
Because many aspects of DAO and ODBC classes are similar, as long as the user has access to ODBC, it is
Easy to learn to use DAO. In fact, users can easily migrate database applications from ODBC to DAO.
Visual C + + provides an example named Daoenrol on the disk, which is actually a enroll
DAO version. The reader can open the Daoenrol project to see, its source code and enroll very similar.
The reader can build the Daoenrol according to the steps to establish enroll, of which only a few places differ, which
The following are the main points:
The selected data source is different. When you create Daoenrol with AppWizard, and when you use ClassWizard to create
CDaoRecordset class, you should select DAO in the Database Options dialog box instead of
Odbc. And DAO's data source is specified by selecting an. mdb file, that is, clicking the "..." button
In the File dialog box, select the that you want to access. MDB file.
The default type for recordsets is different. The default type for an ODBC recordset is a snapshot (Snapshot), while a DAO is a dynamic
State set (Dynaset).
parameterized in different ways. The parameters in the m_strfilter and m_strsort of the DAO Recordset are not "?" Resolution
It's a meaningful parameter name. For example, in the filter below, there is a courseidparam named
Parameters.
M_pset->m_strfilter = "CourseID = Courseidparam";
In the DoFieldExchange function, there are two lines:
Pfx->setfieldtype (CDaoFieldExchange::p Aram);
DFX_Text (PFX, _t ("Courseidparam"), M_strcourseidparam);
The second parameter of the DFX function is also courseidparam.
Handle exceptions in different ways. For example, when you delete a record, the handling of the exception looks like this:
Copy Code code 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 also hide some subtle differences, for example
For example, the DAO recordset is using the DFX Data exchange mechanism (DAO record field Exchange) instead of
RFX, which uses the DFX function instead of the RFX function in the DoFieldExchange of the DAO Recordset.
The characteristics of 10.8.3 DAO
DAO can access ODBC data sources through an ODBC driver. But DAO is based on Microsoft Jet engine
, through which DAO can directly access the access, FoxPro, DBASE, Paradox, Excel, and
Lotus wk and other databases. The CDaoDatabase class can connect directly to these databases without having to
Register DSN in ODBC Manager. For example, the following code is used to open an FoxPro database:
CDaoDatabase Daodb;
Daodb.open ("", False,false, "FoxPro 2.5;database=c:\\zyf");
The Cdaodatabase::open function is used to connect to a database, and the declaration of the function is:
virtual void Open (LPCTSTR lpszname, bool bexclusive = FALSE, bool
Breadonly = FALSE, LPCTSTR lpszconnect = _t (""));
Throw (CDaoException, cmemoryexception);
Parameter bexclusive If True, the function opens the database exclusively, otherwise the shared party
Expression If Breadonly is true, the database is opened as read-only. If you want to open a
Access database, you can specify the MDB file name in the lpszname parameter. If you want to access the number of non access
According to the library, you should make the argument "" and describe a connection string in Lpszconnect. Connection characters
The form of a string is generally "database type; database= path (file)", for example "DBASE III";
Database=c:\\mydir "
The Open function can also open an ODBC data source, but this requires the appropriate ODBC driver and requires
Register DSN in ODBC Manager. At this point the form of Lpszconnect is "Odbc;dsn=mydatasource"
。 Obviously, when you use DAO to access a database like FoxPro, you open it directly rather than using it as an ODBC data source.
It's easy to drive.
Supporting DDL is an important manifestation of DAO's good support for database programming. DDL (Data
Definition Language) is called the "Data definition language" in SQL terminology, which is used to complete the build, repair
The operation of changing and deleting the database structure. The ODBC class supports only DML (Data manipulation Language, number
According to the operating language, DDL is not supported, so the ODBC class can only complete the operation of the data, can not be involved in the database
Structure. To perform DDL operations, only through the ODBC API. The DAO class also provides support for DML and DDL
, which means that programs can use DAO classes to easily create databases and modify the structure of the database.
Compared with ODBC, DAO provides new classes to enhance its functionality, including:
The CDaoTableDef class provides a definition of the structure of a table. Call Cdaotabledef::open to get the table's
Structure definition. Call Cdaotabledef::create can create a new table, calling CDaoTableDef::
CreateField you can add fields to a table, call Cdaotabledef::createindex to add an index to a table
。 Call Cdaotabledef::append to save the newly created table to the database.
The CDaoQueryDef class represents a query definition, which can be stored in the data
Library.
CDaoWorkspace provides a data workspace (Workspace). A workspace can contain several databases,
The workspace can perform all or separate transactions on the database to which it belongs, and the workspace is also responsible for the database's security
Full sex. The program can open multiple workspaces if needed.
Another important feature of DAO is that it provides strong support for Access databases. Because DAO is a base
To the Microsoft Jet engine, so DAO is sure to make some more articles on an Access database. For example
Calling Cdaodatabase::create can directly create an MDB file, as shown in the following code:
m_db. Create ("C:\\mydir\\mydb.") MDB ");
Using AppWizard and ClassWizard, users can easily develop a good performance based on DAO
Access database application.
10.8.4 ODBC or DAO
Because DAO has access to ODBC data sources, the following are the reasons for DAO to replace ODBC:
Better performance can be achieved in some cases, especially when accessing the Microsoft Jet (. MDB) database
When
Compatible with ODBC
DAO allows data to be checked effectively
DAO allows a user to describe the relationship between a table and a table
Of course, the presence of DAO does not mean that ODBC is obsolete. If the user's work must be strictly limited to
ODBC data sources, especially when developing client/server architecture applications, have better sex with ODBC
Yes.