Dao (Database Access Object) uses the Microsoft Jet Database Engine to access the database.
Microsoft Jet provides data engines for products such as access and Visual Basic.
Like ODBC, Dao provides a set of APIS for programming. MFC also provides a set of Dao classes, which encapsulate
The underlying API greatly simplifiesProgramDevelopment. With the DAO class of MFC, you can write
DBMS applications.
Dao was introduced from Visual C ++ 4.0. Generally, the DAO class provides a wider range
Wildcard support. On the one hand, as long as there is an ODBC driver, the DAO of Microsoft Jet can be accessed.
ODBC data source. On the other hand, because Dao is based on the Microsoft Jet Engine
The Access Database (*. MDB File) has good performance.
10.8.2 similarities between Dao and ODBC
There are many similarities between Dao and ODBC, which mainly include the following:
Both support access to various ODBC data sources. Although the two use different data engines, they can be full
It is sufficient for users to write applications independent from DBMS.
Dao provides an MFC class similar to ODBC. For example, the cdaodatabase class of Dao corresponds to
Cdatabase class, cdaorecordset corresponds to crecordset, cdaorecordview corresponds to crecordview
, Cdaoexception corresponds to cdbexception. These classes have similar functions and most of their members
All functions are the same.
Appwizard and classwizard provide similar support for applications that use DAO and ODBC objects.
Because Dao and ODBC are similar in many aspects, as long as you master ODBC
Dao will be used for learning. In fact, users can easily port database applications from ODBC to Dao.
Visual c ++ provides an example named daoenrol, which is actually an enroll
Dao version. You can open the daoenrol project to see itsSource codeSimilar to enroll.
You can follow the steps to create an enroll to create a daoenrol. There are only a few differences.
There are mainly the following points:
The selected data source is different. When you use Appwizard to create a daoenrol and classwizard to create a daoenrol
When a derived class of the cdaorecordset class is used, select Dao instead of Dao in the database Options dialog box.
ODBC. In addition, the DaO data source is specified by selecting a. MDB file, that is, after clicking the "..." button
In the file dialog box, select the. MDB file to be accessed.
The default type of the record set is different. The default type of ODBC record set is snapshot, while Dao
State set (dynaset ).
The parameterization method is different. The parameter in m_strfilter and m_strsort of the DaO record set is not "?" No,
It is a meaningful parameter name. For example, the following filter contains
Parameters.
M_pset-> m_strfilter = "courseid = courseidparam ";
In the dofieldexchange function, there are two rows:
Pfx-> setfieldtype (cdaofieldexchange: Param );
Dfx_text (pfx, _ T ("courseidparam"), m_strcourseidparam );
The second parameter of the DFX function is courseidparam.
Exception Handling methods are different. For example, when deleting a record, the exception is handled as follows:
CopyCode The Code is as follows: Try
{
M_pset-> Delete ();
}
Catch (cdaoexception * E)
{
Afxmessagebox (e->
M_perrorinfo-> m_strdescription );
E-> Delete ();
}
In addition to the preceding differences, Appwizard and classwizard also hide some subtle differences. For example
For example, the DaO record set uses the DFX data exchange mechanism (DAO record field exchange) instead
Rfx uses the DFX function instead of the rfx function in dofieldexchange of the DaO record set.
10.8.3 features of Dao
Dao can access the ODBC data source through the ODBC driver. However, Dao is based on the Microsoft Jet Engine.
With this engine, Dao can directly access Access access, Foxpro, dBase, paradox, Excel and
Databases such as Lotus wk. The cdaodatabase class can be directly connected to these databases without having
Register DSN in 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: The open function is used to connect to a database. The declaration of this function is:
Virtual void open (lpctstr lpszname, bool bexclusive = false, bool
Breadonly = false, lpctstr lpszconnect = _ T (""));
Throw (cdaoexception, cmemoryexception );
If the bexclusive parameter is true, the function opens the database exclusively. Otherwise, the shared party is used.
. If breadonly is true, the database is opened in read-only mode. If you want to open
You can specify the MDB file name in the lpszname parameter. If you want to access a non-access number
The parameter should be set to "" And a connection string should be described in lpszconnect. Connection character
The string format 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 corresponding ODBC driver and requires
Register The DSN In the ODBC manager. In this case, the form of lpszconnect is "ODBC; DSN = mydatasource"
. Obviously, when you use DAO to access a database like FoxPro, you can directly open it as an ODBC data source.
Save time.
DDL is an important embodiment of Dao's good support for database programming. DDL (Data
Definition Language) is called "Data Definition Language" in SQL terms. It is used to complete generation and repair.
Modify and delete the database structure. ODBC class only supports DML (data manipulation language, number
Data operation language), does not support DDL, so the ODBC class can only perform data operations, not involving the database
Structure. To perform DDL operations, you only need to use the odbc api. The DAO class also provides DML and DDL support.
This means that the program can use the DAO class to easily create a database and modify the database structure.
Compared with ODBC, Dao provides some new classes to enhance its functions. These new classes include:
The cdaotabledef class defines the table structure. Call cdaotabledef: open to obtain
Structure definition. Call cdaotabledef: Create to create a new table and call cdaotabledef ::
Createfield can be used to add fields to a table. You can call cdaotabledef: createindex to add indexes 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 data.
Library.
Cdaoworkspace provides a data workspace ). A workspace can contain several databases,
The workspace can process all or individual transactions for the database to which it belongs. The workspace is also responsible for database security
Completeness. If needed, the program can open multiple workspaces.
Another important feature of Dao is that it provides powerful support for access databases. Because Dao is the base
Because of the Microsoft Jet Engine, Dao must do more on the Access database.Article. For example,
Call cdaodatabase: Create to directly create an MDB file. The Code is as follows:
M_db.create ("c: \ mydir \ mydb. mdb ");
With Appwizard and classwizard, you can easily develop Dao-based
Access database application.
10.8.4 ODBC or Dao
Since Dao can access the ODBC data source, the following reasons can be used as Dao to replace ODBC:
In some cases, you can achieve better performance, especially when accessing the Microsoft Jet (. mdb) database.
.
ODBC compatibility
Dao allows effective data check
Dao allows users to describe the relationship between tables.
Of course, the appearance of Dao does not mean that ODBC is out of date. If the user's work must be strictly limited
ODBC data sources, especially when developing client/server applications
Yes.