DAO (Database Access Object) uses the Microsoft Jet database engine to access the database.
Microsoft Jet provides data engines for products like Access and Visual Basic.
Like ODBC, DAO provides a set of APIs for programming use. MFC also provides a set of DAO classes, encapsulating
The underlying API greatly simplifies the development of programs. Using MFC's DAO class, users can write independent
DBMS applications.
DAO was introduced from Visual C++4.0 version. Generally speaking, the DAO class provides a wider range than the ODBC class
Broad support. On the one hand, as long as there is an ODBC driver, you can access it using Microsoft Jet's DAO.
ODBC data source. On the other hand, since DAO is based on the Microsoft Jet engine, it is accessed
Access databases (i.e. *.MDB files) have good performance.
10.8.2 The Similarities between DAO and ODBC
Compared with ODBC classes, DAO classes have many similarities, which are mainly the following points:
Both support access to various ODBC data sources. Although the data engines used by both are different, they can be fully
Sufficient user requirements for writing applications independent of DBMS.
DAO provides MFC classes with similar functions as ODBC. For example, the CDaoDatabase class of DAO corresponds to the ODBC
CDatabase class, CDaoRecordset corresponds to CRecordset, CDaoRecordView corresponds to CRecordView
, CDaoException corresponds to CDBException. These corresponding classes have similar functions, most of their members
The functions are all the same.
AppWizard and ClassWizard provide similar support for applications that use DAO and ODBC objects.
Since many aspects of DAO and ODBC classes are similar, it is very convenient as long as the user masters ODBC
Easy to learn how to use DAO. In fact, users can easily port database applications from ODBC to DAO.
Visual C++ provides an example called DaoEnrol, which is actually an Enroll
DAO version. Readers can open the DaoEnrol project and take a look. Its source code is very similar to Enroll's.
Readers can follow the steps of establishing Enroll to create DaoEnrol, where only a few places are different,
There are mainly the following points:
The selected data source is different. When creating DaoEnrol with AppWizard and when creating ClassWizard
When derived classes of CDaoRecordset class, DAO should be selected instead of
ODBC. Moreover, the DAO data source is specified by selecting a .MDB file, that is, after clicking the "..." button
Select the .MDB file to access in the File dialog box.
The default types of recordsets are different. The default type of ODBC record set is Snapshot, while DAO is active
Status set (Dynaset).
The parameterization method is different. The parameters in m_strFilter and m_strSort of the DAO record set are not "?" signs.
It is a meaningful parameter name. For example, in the filter below there is a CourseIDParam
parameter.
m_pSet->m_strFilter ="CourseID = CourseIDParam";
In the DoFieldExchange function, there are two lines:
pFX->SetFieldType(CDaoFieldExchange::param);
DFX_Text(pFX, _T("CourseIDParam"), m_strCourseIDParam);
The second parameter of the DFX function is also CourseIDParam.
The exceptions are handled differently. For example, when deleting a record, the exception is handled 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, such as
For example, the DAO record set uses the DFX data exchange mechanism (DAO record field exchange) instead of
RFX, the DFX function is used instead of the RFX function in the DoFieldExchange of the DAO record set.
10.8.3 Features of DAO
DAO can access ODBC data sources through the ODBC driver. But DAO is based on Microsoft Jet engine
, through this engine, DAO can directly access 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 a FoxPro database:
CDaoDatabase daoDb;
( “”,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 );
If the parameter bExclusive is TRUE, the function opens the database exclusively, otherwise the sharer will be used.
Mode. If bReadOnly is TRUE, then the database is opened in read-only. If you want to open one
Access database, you can specify the MDB file name in the lpszName parameter. If you want to access a non-Access number
The parameter should be "" and a connection string should be described in lpszConnect. Connect characters
The form of a string is generally "database type; DATABASE=path (file)", such as "dBASE III;
DATABASE=c:\\MYDIR”
The Open function can also open an ODBC data source, but this requires the corresponding ODBC driver and requires
Register DSN in ODBC Manager. At this time, the form of lpszConnect is "ODBC; DSN=MyDataSource"
. Obviously, when using DAO to access a database like FoxPro, it is better to open it directly than to use it as an ODBC data source.
It is necessary to save trouble.
Supporting DDL is an important manifestation of DAO's good support for database programming. DDL(Data
Definition Language) is called "data definition language" in SQL terms, which is used to complete generation and repair.
Operations to modify and delete database structures. ODBC class only supports DML (Data Manipulation Language, number
According to the operation language), DDL is not supported, so using ODBC class can only complete data operations and cannot involve database operations.
structure. To perform DDL operations, only through the ODBC API. The DAO class provides support for both DML and DDL
, which means that the program can use the DAO class to easily create and modify the database structure.
Compared with ODBC, DAO provides some new classes to enhance its functionality, including:
The CDaoTableDef class provides definitions of the structure of a table. Call CDaoTableDef::Open to get the table
Structural definition. Calling CDaoTableDef::Create can create a new table, and calling CDaoTableDef::
CreateField can add fields to tables, and calling CDaoTableDef::CreateIndex can add indexes to tables
. Calling CDaoTableDef::Append can save the newly created table to the database.
The CDaoQueryDef class represents a query definition (Query definition), which can be stored in data.
in the library.
CDaoWorkspace provides a data workspace. A workspace can contain several databases.
The workspace can perform all or separate transactions on the database it belongs to, and the workspace is also responsible for the database installation.
Totality. If needed, the program can open multiple workspaces.
Another important feature of DAO is its powerful support for Access databases. Since DAO is
From Microsoft Jet Engine, DAO must write more articles on the Access database. For example,
Calling CDaoDatabase::Create can directly create an MDB file, the code is as follows:
m_db.Create(“C:\\MYDIR\\”);
With AppWizard and ClassWizard, users can easily develop DAO-based
Access database application.
10.8.4 ODBC or DAO
Since DAO can access ODBC data sources, the following reasons can be used as a reason for DAO to replace ODBC:
Better performance can be achieved in some cases, especially when accessing Microsoft Jet (.MDB) database
hour.
Compatible with ODBC
DAO allows data to be checked effectively
DAO allows users to explain the relationship between tables
Of course, the emergence of DAO does not mean that ODBC is outdated. If the user's work must be strictly limited to
ODBC data source, especially when developing applications with Client/Server structure, ODBC is better
able.
Microsoft Jet provides data engines for products like Access and Visual Basic.
Like ODBC, DAO provides a set of APIs for programming use. MFC also provides a set of DAO classes, encapsulating
The underlying API greatly simplifies the development of programs. Using MFC's DAO class, users can write independent
DBMS applications.
DAO was introduced from Visual C++4.0 version. Generally speaking, the DAO class provides a wider range than the ODBC class
Broad support. On the one hand, as long as there is an ODBC driver, you can access it using Microsoft Jet's DAO.
ODBC data source. On the other hand, since DAO is based on the Microsoft Jet engine, it is accessed
Access databases (i.e. *.MDB files) have good performance.
10.8.2 The Similarities between DAO and ODBC
Compared with ODBC classes, DAO classes have many similarities, which are mainly the following points:
Both support access to various ODBC data sources. Although the data engines used by both are different, they can be fully
Sufficient user requirements for writing applications independent of DBMS.
DAO provides MFC classes with similar functions as ODBC. For example, the CDaoDatabase class of DAO corresponds to the ODBC
CDatabase class, CDaoRecordset corresponds to CRecordset, CDaoRecordView corresponds to CRecordView
, CDaoException corresponds to CDBException. These corresponding classes have similar functions, most of their members
The functions are all the same.
AppWizard and ClassWizard provide similar support for applications that use DAO and ODBC objects.
Since many aspects of DAO and ODBC classes are similar, it is very convenient as long as the user masters ODBC
Easy to learn how to use DAO. In fact, users can easily port database applications from ODBC to DAO.
Visual C++ provides an example called DaoEnrol, which is actually an Enroll
DAO version. Readers can open the DaoEnrol project and take a look. Its source code is very similar to Enroll's.
Readers can follow the steps of establishing Enroll to create DaoEnrol, where only a few places are different,
There are mainly the following points:
The selected data source is different. When creating DaoEnrol with AppWizard and when creating ClassWizard
When derived classes of CDaoRecordset class, DAO should be selected instead of
ODBC. Moreover, the DAO data source is specified by selecting a .MDB file, that is, after clicking the "..." button
Select the .MDB file to access in the File dialog box.
The default types of recordsets are different. The default type of ODBC record set is Snapshot, while DAO is active
Status set (Dynaset).
The parameterization method is different. The parameters in m_strFilter and m_strSort of the DAO record set are not "?" signs.
It is a meaningful parameter name. For example, in the filter below there is a CourseIDParam
parameter.
m_pSet->m_strFilter ="CourseID = CourseIDParam";
In the DoFieldExchange function, there are two lines:
pFX->SetFieldType(CDaoFieldExchange::param);
DFX_Text(pFX, _T("CourseIDParam"), m_strCourseIDParam);
The second parameter of the DFX function is also CourseIDParam.
The exceptions are handled differently. For example, when deleting a record, the exception is handled as follows:
Copy the codeThe code is 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, such as
For example, the DAO record set uses the DFX data exchange mechanism (DAO record field exchange) instead of
RFX, the DFX function is used instead of the RFX function in the DoFieldExchange of the DAO record set.
10.8.3 Features of DAO
DAO can access ODBC data sources through the ODBC driver. But DAO is based on Microsoft Jet engine
, through this engine, DAO can directly access 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 a FoxPro database:
CDaoDatabase daoDb;
( “”,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 );
If the parameter bExclusive is TRUE, the function opens the database exclusively, otherwise the sharer will be used.
Mode. If bReadOnly is TRUE, then the database is opened in read-only. If you want to open one
Access database, you can specify the MDB file name in the lpszName parameter. If you want to access a non-Access number
The parameter should be "" and a connection string should be described in lpszConnect. Connect characters
The form of a string is generally "database type; DATABASE=path (file)", such as "dBASE III;
DATABASE=c:\\MYDIR”
The Open function can also open an ODBC data source, but this requires the corresponding ODBC driver and requires
Register DSN in ODBC Manager. At this time, the form of lpszConnect is "ODBC; DSN=MyDataSource"
. Obviously, when using DAO to access a database like FoxPro, it is better to open it directly than to use it as an ODBC data source.
It is necessary to save trouble.
Supporting DDL is an important manifestation of DAO's good support for database programming. DDL(Data
Definition Language) is called "data definition language" in SQL terms, which is used to complete generation and repair.
Operations to modify and delete database structures. ODBC class only supports DML (Data Manipulation Language, number
According to the operation language), DDL is not supported, so using ODBC class can only complete data operations and cannot involve database operations.
structure. To perform DDL operations, only through the ODBC API. The DAO class provides support for both DML and DDL
, which means that the program can use the DAO class to easily create and modify the database structure.
Compared with ODBC, DAO provides some new classes to enhance its functionality, including:
The CDaoTableDef class provides definitions of the structure of a table. Call CDaoTableDef::Open to get the table
Structural definition. Calling CDaoTableDef::Create can create a new table, and calling CDaoTableDef::
CreateField can add fields to tables, and calling CDaoTableDef::CreateIndex can add indexes to tables
. Calling CDaoTableDef::Append can save the newly created table to the database.
The CDaoQueryDef class represents a query definition (Query definition), which can be stored in data.
in the library.
CDaoWorkspace provides a data workspace. A workspace can contain several databases.
The workspace can perform all or separate transactions on the database it belongs to, and the workspace is also responsible for the database installation.
Totality. If needed, the program can open multiple workspaces.
Another important feature of DAO is its powerful support for Access databases. Since DAO is
From Microsoft Jet Engine, DAO must write more articles on the Access database. For example,
Calling CDaoDatabase::Create can directly create an MDB file, the code is as follows:
m_db.Create(“C:\\MYDIR\\”);
With AppWizard and ClassWizard, users can easily develop DAO-based
Access database application.
10.8.4 ODBC or DAO
Since DAO can access ODBC data sources, the following reasons can be used as a reason for DAO to replace ODBC:
Better performance can be achieved in some cases, especially when accessing Microsoft Jet (.MDB) database
hour.
Compatible with ODBC
DAO allows data to be checked effectively
DAO allows users to explain the relationship between tables
Of course, the emergence of DAO does not mean that ODBC is outdated. If the user's work must be strictly limited to
ODBC data source, especially when developing applications with Client/Server structure, ODBC is better
able.