📄 vcg13.htm
字号:
<HTML>
<HEAD>
<TITLE>vcg13.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/htindex.htm">
<LINK REL="Next" HREF="vcg14.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg14.htm">
<LINK REL="Previous" HREF="vcgp4.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp4.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A><P ALIGN=CENTER>
<A HREF="vcgp4.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp4.htm" TARGET="_self"><IMG SRC="blanprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm" TARGET="_self"><IMG SRC="blantoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>
<A HREF="vcg14.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg14.htm" TARGET="_self"><IMG SRC="blannext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E73" >The DAO MFC Classes</A>
<UL>
<LI>
<A HREF="#E69E195" >CDaoWorkspace</A>
<LI>
<A HREF="#E69E196" >CDaoDatabase</A>
<LI>
<A HREF="#E69E197" >CDaoTableDef</A>
<LI>
<A HREF="#E69E198" >CDaoQueryDef</A>
<LI>
<A HREF="#E69E199" >CDaoRecordset</A>
<LI>
<A HREF="#E69E200" >CDaoException</A>
<LI>
<A HREF="#E69E201" >CDaoFieldExchange</A>
<LI>
<A HREF="#E69E202" >CDaoRecordView</A></UL>
<LI>
<A HREF="#E68E74" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E35"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>- 13 -</B>
<BR><B>Understanding MFC's DAO Classes</B></FONT></CENTER></H1>
<BR>
<P>Until Microsoft created Visual C++ 4, database programmers had some difficulty justifying Visual C++ as a mainstream database development platform. Visual C++'s sole support for database developers was ODBC. Visual C++ didn't support the more powerful (and useful) Microsoft Jet database engine. In fact, Visual C++ programmers often were told by Visual Basic programmers that they had a better database development platform. Today, with Visual C++ 4, Visual C++ programmers are no longer at such a disadvantage. They now have access to the Microsoft Jet database engine and DAO (Database Access Objects).
<BR>
<P><B>WARNING</B>
<BR>
<BR>The DAO database classes are meant for single-threaded operation only. Don't try to utilize these classes in more than one thread at one time.
<P>With Visual C++ 4, the DAO database classes are considered distinct from the MFC ODBC database classes. All of the DAO database class names have a prefix of CDao. Usually the DAO classes are more powerful than the ODBC classes. DAO classes use the Microsoft Jet database engine to access data. In turn, the Microsoft Jet database engine uses ODBC drivers to access data that isn't supported directly. The DAO classes also support the Data Definition Language (DDL) operations—for example, operations for creating databases and adding tables and fields via the classes—all without having to call DAO directly.
<BR>
<P>This chapter documents the new MFC classes that support the DAO interface. These classes (seven main new classes and several more supporting classes and structures) are very similar to their ODBC database counterparts, such as CDatabase and CRecordset.
<BR>
<P>Generally, the majority of Visual C++ database applications will be written using AppWizard and will be expanded by the programmer to become complete, working database applications. It's possible to write a database application from scratch using the DAO database classes—and a few programmers do. However, neither Microsoft nor the authors recommend doing this. With this in mind, your database application will have a few of the classes included by AppWizard (these classes are described next). During the development cycle, you will probably add other classes.
<BR>
<P>The MFC classes for DAO are like all other MFC classes. They serve as wrappers, encapsulating DAO's basic functionality. For example:
<BR>
<UL>
<LI>The CDaoWorkspace class encapsulates the functionality of the DAO Workspace object.
<BR>
<BR>
<LI>The CDaoRecordset class encapsulates the functionality of the DAO Recordset object.
<BR>
<BR>
<LI>The CDaoDatabase class encapsulates the functionality of the DAO Database object.
<BR>
<BR>
</UL>
<P>This process continues through the other DAO MFC classes.
<BR>
<P>Not all of the DAO functionality is encapsulated in the DAO MFC classes. You'll find that the major DAO objects are supported, but some objects, such as fields, indexes, parameters, and relations, aren't directly supported by the DAO MFC objects.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>There are three technical papers (called Technical Notes) specific to DAO:</NOTE>
<BR>
<BR>
<CENTER><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
TN053
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Writing Custom DFX Routines for DAO Database Classes
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
TN054
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Calling DAO Directly While Using MFC DAO Classes
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
TN055
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Migrating MFC ODBC Database Class Applications to MFC DAO Classes</FONT>
</TABLE></CENTER>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Technical Note 54 (in the Visual C++ Books Online, see Visual C++ Books, MFC 4.0, MFC Technical Notes) has an example of using nonencapsulated DAO functionality in an MFC application.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Table 13.1 shows a mapping of DAO objects to MFC classes. The Comments and Related ODBC Classes column lists the corresponding ODBC class when applicable.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 13.1. The MFC-to-DAO object map.</B></FONT></CENTER>
<BR>
<CENTER><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>MFC Class</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>DAO Object</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Comments and Related </I><I>ODBC Classes</I>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoWorkspace
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Workspace
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Manages the transaction space. Provides the Microsoft Jet database engine access.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoDatabase
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Database
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Represents a connection to a specific database. Similar to CDatabase.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoTableDef
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Tabledef
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Determines and manipulates the structure of a given table.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoQueryDef
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Querydef
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Saves queries in a database.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoRecordset
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Recordset
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Manages the result set, a set of records based on a table or selected by a query. Similar to CRecordset.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoException
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Error
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
MFC responds to all DAO errors by throwing exceptions of this type.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoFieldExchange
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
None
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Manages the exchange of data between a record in the database and the field data members of a recordset. Similar to CFieldExchange.</FONT>
</TABLE></CENTER><BR>
<A NAME="E68E73"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The DAO MFC Classes</B></FONT></CENTER></H3>
<BR>
<P>The following sections document the DAO MFC classes by showing significant member functions and giving examples of usage.
<BR>
<P>As I mentioned earlier, some of the DAO MFC classes have ODBC analogues. This makes it easier to convert ODBC applications to use the DAO MFC classes. Generally, if you're unsure whether you should use DAO or ODBC, you should choose DAO to base your application on if the underlying database structure is either Access or one of the database structures that DAO supports directly (such as FoxPro).
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>All DAO functions require the DAO header. Use the statement #include <afxdao.h>. When you use AppWizard to create an application, the #include is added automatically.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>See the DAO MFC documentation for more information about each of the DAO MFC classes. Simply search for the class name in the Books Online in Visual C++.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E195"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B><I>CDaoWorkspace</I></B></FONT></CENTER></H4>
<BR>
<P>The CDaoWorkspace object is the highest object in the DAO hierarchy. It represents the DAO Workspace object. The DAO workspace defines the session for the user. It contains open databases and support for simultaneous transactions.
<BR>
<P>The CDaoWorkspace object manages named database sessions (from initial logon to logoff) for a single user. This database may be password protected. Generally, an application will use a single workspace. With AppWizard applications, it's unnecessary to create an explicit Workspace object. If your application needs multiple sessions, you can create additional Workspace objects. For each Workspace object, there may be more than one open database object at any given time. Under MFC's implementation, the workspace is basically a manager of transactions.
<BR>
<P>The CDaoWorkspace class provides the following functionality:
<BR>
<UL>
<LI>It provides explicit access to a default workspace. This default workspace is created by initializing the database engine. Typically, you would use CDaoWorkspace's default workspace implicitly by creating DAO database and recordset objects.
<BR>
<BR>
<LI>It provides a transaction space where transactions will be applied to all databases that are open in the workspace. There are no restrictions in creating additional workspaces to manage separate transaction spaces.
<BR>
<BR>
<LI>It provides the interface to many of the properties of the Microsoft Jet database engine. The database engine can be initialized by opening or creating a workspace. The database engine can also be initialized by calling a static class member function before an open or create call.
<BR>
<BR>
<LI>It provides access to the Workspaces collection of the database engine. All active workspaces that have been appended to it will be stored in the Workspaces collection. It's also possible to create and work with workspaces without having to append a workspace to a Workspaces collection.
<BR>
<BR>
</UL>
<P>The DAO MFC classes don't implement the security control Users and Groups collections. When security is a concern, you will need to provide the necessary interface using direct calls to the underlying DAO interface.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>For information on accessing the underlying DAO interface, go to Books Online and search for the topic Technical Note 54.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The CDaoWorkspace call may be used to do the following things:
<BR>
<UL>
<LI>Open the default workspace explicitly. Typically, the opening of the default workspace is implicit—for example, when you open a new CDaoDatabase or CDaoRecordset object. However, a situation might arise where you have to access the default workspace explicitly—for example, to access database engine properties or the Workspaces collection.
<BR>
<BR>
<LI>Create a new workspace. If you want to add new workspaces, you would call Append.
<BR>
<BR>
<LI>Open an existing workspace that is in the Workspaces collection.
<BR>
<BR>
</UL>
<P>In order to use CDaoWorkspace to create a new workspace that doesn't exist in the Workspaces collection, use the CDaoWorkspace.Create() member function. With CDaoWorkspace, workspace objects won't persist between Microsoft Jet database engine sessions. If your application links MFC statically (that is, you link the MFC code using a static library rather than a DLL), when the application ends, the Microsoft Jet database engine will still be initialized. However, if the application uses MFC using the MFC DLL, the Microsoft Jet database engine will be uninitialized when the MFC DLL is unloaded.
<BR>
<P>Use the Open() member function to open an existing workspace. Use the Close() member function to close a workspace session. When Close() is called, all databases that haven't already been closed will be closed. Also, all uncommitted transactions will be rolled back (discarded).
<BR>
<P>DAO is used to manage transactions at the workspace level. As such, any transactions to workspaces with more than one open database will be applied to all open databases. For example, if there are two databases with uncommitted updates, and there is a call to CommitTrans(), all of the updates are committed, regardless of which database the changes are being applied to. When it's necessary to limit your transactions to one of a number of open databases, you must create a separate workspace object for the database to which you wish to limit your transactions.
<BR>
<P>Your application will use the DAO default workspace implicitly when the following events occur:
<BR>
<UL>
<LI>When a new CDaoDatabase object is created without using an existing CDaoWorkspace object, CDaoWorkspace will create a temporary workspace object for you. This temporary workspace will correspond to DAO's default workspace. This is true even when done with multiple databases—all the database objects will be associated with the default workspace. The database's workspace may then be accessed using the CDaoDatabase member.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -