📄 vcg14.htm
字号:
<HTML>
<HEAD>
<TITLE>vcg14.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="vcg15.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg15.htm">
<LINK REL="Previous" HREF="vcg13.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg13.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A><P ALIGN=CENTER>
<A HREF="vcg13.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg13.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="vcg15.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg15.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="#E68E75" >An AppWizard-Created DAO Application</A>
<UL>
<LI>
<A HREF="#E69E203" >Creating a Template Database</A>
<LI>
<A HREF="#E69E204" >Using AppWizard to Create a DAO Application</A>
<LI>
<A HREF="#E69E205" >Binding Variables to Dialog Controls</A>
<LI>
<A HREF="#E69E206" >Implementing the File Menu</A>
<UL>
<LI>
<A HREF="#E70E68" >Opening and Closing a DAO Access Database</A>
<LI>
<A HREF="#E70E69" >Creating a New DAO Access Database</A>
<LI>
<A HREF="#E70E70" >Adding Indexes to a DAO Database</A>
<LI>
<A HREF="#E70E71" >Adding Records to a DAO Database</A></UL></UL>
<LI>
<A HREF="#E68E76" >Using CDaoRecordset Directly in a Dialog Box</A>
<LI>
<A HREF="#E68E77" >Using DAO Dynamically</A>
<LI>
<A HREF="#E68E78" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E37"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>- 14 -</B>
<BR><B>Using MFC's DAO Classes</B></FONT></CENTER></H1>
<BR>
<P>In Chapter 13, "Understanding MFC's DAO Classes," you looked at the new MFC 4 DAO data access classes. This chapter introduces you to these classes in actual working code. You'll create several examples of DAO applications using as many features of DAO as you can.
<BR>
<P>This chapter is divided into three parts. In the first part you'll create a standard DAO application using AppWizard. In the second part, you'll look at using each of the DAO MFC classes:
<BR>
<UL>
<UL>
<P>CDaoDatabase
</UL></UL>
<UL>
<UL>
<P>CDaoException
</UL></UL>
<UL>
<UL>
<P>CDaoFieldExchange
</UL></UL>
<UL>
<UL>
<P>CDaoQueryDef
</UL></UL>
<UL>
<UL>
<P>CDaoRecordset
</UL></UL>
<UL>
<UL>
<P>CDaoRecordView
</UL></UL>
<UL>
<UL>
<P>CDaoTableDef
</UL></UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>CDaoWorkspace
<BR>
</BLOCKQUOTE></BLOCKQUOTE>
<P>In the final part of this chapter, you'll create a program that returns information about an Access database using calls to DAO. If possible, you should let AppWizard design the shell of your application. Generally, for simple front-end applications, AppWizard provides an excellent framework for your program and requires only minor filling out by you—usually only the user interface. For more complex applications, you probably will use AppWizard to create the application's shell and add the DAO support later by hand.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>This chapter uses two sample programs. The first part of this chapter uses the Addresses program, and the second part uses the DAO Direct program. Both of these programs use the Addresses Access database, found in the CHAPTR14 directory on the CD that comes with this book.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E75"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>An AppWizard-Created DAO Application</B></FONT></CENTER></H3>
<BR>
<P>In this, the first part of the chapter, you'll create an application using AppWizard, with support for DAO. Using DAO in this manner lets you create an application that looks (and feels) like an ODBC application, both from the programmer's viewpoint and from the user's viewpoint.
<BR>
<P>The main differences in a stock AppWizard DAO application and a stock AppWizard ODBC application are the names of the classes used. Generally, the functions and functionalities of DAO and ODBC are virtually identical. For example, there is a one-to-one correspondence between DAO classes and the ODBC classes, as shown in Table 14.1.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 14.1. Correspondence between MFC's DAO and ODBC classes.</B></FONT></CENTER>
<BR>
<CENTER><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>DAO Class</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>ODBC Class</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Functionality</I>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoDatabase
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDatabase
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A CDaoDatabase (and CDatabase) class object represents a connection to a database through which you can operate on the data.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoException
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDBException
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The CDaoException class object represents an exception condition raised by one of the MFC DAO database classes.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoFieldExchange
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CFieldExchange
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
The CDaoFieldExchange (and CFieldExchange) class object supports record field exchange (DFX). Typically directly used when you're writing data exchange routines for custom data types. Otherwise, AppWizard and ClassWizard will manipulate this class for you. Technical Note 53, available under MFC in Books Online, contains the necessary information for writing your own custom DFX routines.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoQueryDef
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
No ODBC equivalent
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A CDaoQueryDef class object represents a query definition that is typically saved in a database.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoRecordset
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CRecordset
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A CDaoRecordset (and CRecordset) class object represents a set of records selected from a data source.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoRecordView
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CRecordView
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A CDaoRecordView (and CRecordView) class object is used to represent a view that displays database records in controls (such as in a CFormView-based application).
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoTableDef
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
No ODBC equivalent
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A CDaoTableDef class object represents the stored definition of a base table or an attached table.
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
CDaoWorkspace
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
No ODBC equivalent
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A CDaoWorkspace class object manages a named, password-protected database session from logon to logoff by a given user.</FONT>
</TABLE></CENTER><P>Since DAO is intended to work with Access databases (after all, DAO uses the Access Jet database engine), you need to have at least one Access database installed on your system prior to using AppWizard in order to generate a DAO MFC application. Finding an Access database isn't difficult. If nothing else is available, you can use one of the sample databases (such as Northwind.mdb, typically found in the \MSOffice\Access\Samples directory) to get started writing DAO applications.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>It's not necessary to register your Access database (using ODBC) when you're creating or using a DAO application. DAO doesn't use ODBC.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>When creating your application, you will have to select the basic user interface. Select either MDI or SDI, but not dialog-based, because a dialog-based application can't have database access when generated using AppWizard. Once the user interface is configured, you must select the degree of database support. You have the standard levels regardless of whether your application will be ODBC or DAO. The levels that may be selected include header files only, database view without file support, and database view with file support. If your application won't be using a separately saved document file, selecting database view without file support will create the best application with the least amount of clutter. Selecting header files only will create an application that may have DAO or ODBC support added later.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Which came first, the chicken or the egg? In creating your application, you face your first dilemma: You need an existing database in order for AppWizard to generate your DAO application, but the application will generate the database using File | New.
<BR>
<BR>The solution is relatively simple: You will create a template database using Access. This will allow you to design your database using Access's tools. Then later you can add code to the application to create new databases as needed.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E203"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Creating a Template Database</B></FONT></CENTER></H4>
<BR>
<P>A template database is needed in order for Visual C++ 4's AppWizard to create a DAO project. I used Access 7 to create a new database using Access's Database Wizard. The Database Wizard's Address Book template was chosen as the type of database desired because it fit the needs of your sample application, a names database.
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -