📄 vcg02.htm
字号:
<HTML>
<HEAD>
<TITLE>vcg02.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="vcg03.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg03.htm">
<LINK REL="Previous" HREF="vcg01.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg01.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A><P ALIGN=CENTER>
<A HREF="vcg01.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg01.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="vcg03.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg03.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="#E68E22" >Defining the Characteristics of Data-Related Objects and Classes</A>
<LI>
<A HREF="#E68E23" >The MFC Database Classes</A>
<UL>
<LI>
<A HREF="#E69E38" >CDatabase</A>
<UL>
<LI>
<A HREF="#E70E3" >Data Members</A>
<LI>
<A HREF="#E70E4" >Construction</A>
<LI>
<A HREF="#E70E5" >Database Attributes</A>
<LI>
<A HREF="#E70E6" >Database Operations</A>
<LI>
<A HREF="#E70E7" >Database Overridables</A></UL>
<LI>
<A HREF="#E69E39" >CRecordset</A>
<UL>
<LI>
<A HREF="#E70E8" >Data Members</A>
<LI>
<A HREF="#E70E9" >Construction/Destruction</A>
<LI>
<A HREF="#E70E10" >Recordset Attributes</A>
<LI>
<A HREF="#E70E11" >Recordset Update Operations</A>
<LI>
<A HREF="#E70E12" >Recordset Navigation Operations</A>
<LI>
<A HREF="#E70E13" >Other Recordset Operations</A>
<LI>
<A HREF="#E70E14" >Recordset Overridables</A></UL>
<LI>
<A HREF="#E69E40" >CRecordView</A>
<UL>
<LI>
<A HREF="#E70E15" >Construction</A>
<LI>
<A HREF="#E70E16" >Attributes</A>
<LI>
<A HREF="#E70E17" >Operations</A></UL>
<LI>
<A HREF="#E69E41" >CFieldExchange</A>
<LI>
<A HREF="#E69E42" >CDBException</A>
<UL>
<LI>
<A HREF="#E70E18" >Data Members</A></UL>
<LI>
<A HREF="#E69E43" >CLongBinary</A>
<UL>
<LI>
<A HREF="#E70E19" >Data Members</A>
<LI>
<A HREF="#E70E20" >Construction Member</A></UL>
<LI>
<A HREF="#E69E44" >RFX Functions</A>
<LI>
<A HREF="#E69E45" >An AppWizard-Generated Program</A>
<UL>
<LI>
<A HREF="#E70E21" >CRecordView Support</A>
<LI>
<A HREF="#E70E22" >CRecordset Support</A></UL></UL>
<LI>
<A HREF="#E68E24" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E7"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>- 2 -</B>
<BR><B>Understanding MFC's Data Access Classes</B></FONT></CENTER></H1>
<BR>
<P>The MFC data access classes are Visual C++'s object-oriented method of interacting with datasources. MFC's implementation of ODBC supports three major objects: CDatabase, CRecordView, and CRecordset. Supporting classes include CDBException, CFieldExchange, and CLongBinary. Most commonly, programmers use these objects when working with applications created with Visual C++'s AppWizard program. Any database application created by using AppWizard will incorporate these classes.
<BR>
<P>Chapter 1 introduced Visual C++ and accessing databases. This chapter describes the structure of the MFC data access classes in detail because the member functions of these classes constitute the foundation on which all of your Visual C++ MFC database applications are built. This chapter features examples that use the member functions to create Visual C++ code. By the time you complete this rather lengthy chapter, it's very likely that you will have learned more than you ever wanted to know about data-related objects and classes!
<BR>
<P>Programmers who want to "roll their own" and use the database classes will have few (if any) problems incorporating them into their applications. However, for a simple front-end application in which data access and updating are the main functions of the program, using AppWizard is the simplest choice. The sample program shown in Figure 2.1 took only about 10 minutes to write and required no manual source code modification to create. The source code for this program is on the CD that comes with this book (see the directory CHAPTR02\Record View). Take a look at the program to see how simple it is to create a quick ODBC application.
<BR>
<P><B><A HREF="02vcg01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/02vcg01.gif">Figure 2.1. A CRecordView-based application.</A></B>
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Also found on the CD is a 16-bit MFC 2.5 version of the same program, which is in the directory CHAPTR02\RECVIEW. The RECVIEW program should be built only by using Visual C++ 1.5x.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E22"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Defining the Characteristics of Data-Related Objects and Classes</B></FONT></CENTER></H3>
<BR>
<P>In the object-oriented terminology of OLE, <I>objects</I> are containers for properties, methods, and other objects. Methods are called <I>member </I><I>functions</I> of an object. They perform an action on the object, such as changing its color, size, or shape. <I>Properties</I> are member function pairs of a <I>programmable</I> object. You can set or return information about the state of a programmable object, such as the value of a data item in the field of a table. One member function sets the data, and another member function returns the data—thus, the term <I>pair</I>. All of the member functions are said to be <I>encapsulated</I> in the object.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>Technically, you should be able to alter any property of a programmable object by assigning an appropriate value to the "set" member of the function pair. The ability to set property values under specific conditions depends on the type of object and the application in which the object is used. Access 1.x, for example, has many objects whose properties can be set only in design mode.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E23"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The MFC Database Classes</B></FONT></CENTER></H3>
<BR>
<P>The MFC database classes may be implemented by the programmer or by AppWizard when it creates an application; in either case, these classes are easy to use.
<BR>
<P>There are differences between MFC 2.5 (the 16-bit version, used with Visual C++ 1.5x), MFC 3.0 (a 32-bit version, used with Visual C++ 2.x), and MFC 4, which is supplied with Visual C++ 4.0. Where significant differences exist, I describe both. Most differences are in between the 16-bit MFC 2.5 and the later 32-bit versions. In keeping with the concept of compatibility, generally an application written for MFC 2.5 will run without error when recompiled by using one of the 32-bit versions of Visual C++. However, after you've implemented any of the enhancements from MFC 3.0 (or MFC 4) in a 16-bit MFC 2.5 application (and converted it to 32-bits), you won't be able to build that application using Visual C++ 1.5!
<BR>
<BR>
<A NAME="E69E38"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B><I>CDatabase</I></B></FONT></CENTER></H4>
<BR>
<P>The CDatabase class object is used to encapsulate a connection to a database. The CDatabase object may then be used to operate on the database and is derived from the CObject base class. Figure 2.2 shows the class hierarchy for the CDatabase class.
<BR>
<P><B><A HREF="02vcg02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/02vcg02.gif">Figure 2.2. The CDatabase class hierarchy.</A></B>
<BR>
<P>The CDatabase class object has a number of member functions. These functions are divided into the following five categories:
<BR>
<UL>
<LI>Data members: The data members of the CDatabase class hold information that is used when you're working directly with the database that the CDatabase object has been attached to.
<BR>
<BR>
<LI>Construction: The constructor and a set of database open/close functions form the construction members.
<BR>
<BR>
<LI>Database attributes: Nine functions are used to obtain information about the database that the CDatabase object has been attached to.
<BR>
<BR>
<LI>Database operations: The five database operation functions allow for transaction processing and the execution of direct SQL commands.
<BR>
<BR>
<LI>Database overrides: Two overridable functions are provided to let the programmer customize the functionality of the CDatabase object.
<BR>
<BR>
</UL>
<P>The following sections take a closer look at the members of this class. The members of the CObject class (which CDatabase is derived from) aren't covered in this book. Refer to the Visual C++ documentation (either the manuals or the online help system) for full information about the CObject class.
<BR>
<BR>
<A NAME="E70E3"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Data Members</B></FONT></CENTER></H5>
<BR>
<P>The CDatabase object contains only one data member.
<BR>
<P>The m_hdbc member variable contains the ODBC connection handle to the database that is currently open. If no database is currently open, this member variable doesn't contain useful data.
<BR>
<P>The m_hdbc member variable is of type HDBC. It can be used wherever an HDBC type variable is needed (for example, in one of the SQL...() functions).
<BR>
<P>Here's an example of the usage of m_hdbc:
<BR>
<PRE>
<FONT COLOR="#000080">nReturnCode = ::SQLGetInfo(cdb.m_hdbc, SQL_ODBC_SQL_CONFORMANCE,
&nReturn, sizeof(nReturn), &cbValue);</FONT></PRE>
<P>In this example, a call to a function that isn't a member function of CDatabase is made by using the m_hdbc member data variable.
<BR>
<BR>
<A NAME="E70E4"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Construction</B></FONT></CENTER></H5>
<BR>
<P>Three member functions deal directly with CDatabase construction: CDatabase(), Open(), and Close(). There also is a default destructor, which I don't document here because it's never called by an application. The following paragraphs describe each construction member function and, where applicable, give examples of usage.
<BR>
<P>The CDatabase()<B> </B>function is used to construct the CDatabase object. This function isn't called directly and has no parameters. The process of calling the constructor is taken care of during the initialization of the CDatabase object when it's created. Here is a typical creation of a CDatabase object (this code is usually in the header file for the document class):
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">CDatabase m_dbCustomerDB; // No parameters</FONT></PRE>
<P>When creating a CDatabase object, your application must make sure that the CDatabase object is connected to a database. This is accomplished in a member function in the containing class, often called GetDatabase() (if the containing class is based on a CDocument type object).
<BR>
<P>If you call CDatabase::Open(), passing a NULL as the <I>lpszDSN</I> parameter, the user will be presented with an open datasource dialog box. The Record View sample program for this chapter shows this dialog box (see Figure 2.1).
<BR>
<PRE>
<FONT COLOR="#000080">CDatabase* CMyDoc::GetDatabase()
{// Returns NULL in the event of a failure!
// m_dbCustomerDB is a member of CMyDoc!
// Connect the object to a database
if(!m_dbCustomerDB.IsOpen() && !m_dbCustomerDB.Open(NULL))
(// The database cannot be opened; we've failed!
return(NULL);
}
else
{// We already had a database, or opened one:
return(&m_dbCustomerDB);
}
}</FONT></PRE>
<P>The Open() member function is used to establish a connection to a database. This connection is established through an ODBC driver. The Open() function takes a number of parameters. Here's the prototype of the MFC 2.5 version of the Open() function:
<BR>
<PRE>
<FONT COLOR="#000080">BOOL Open( LPCSTR lpszDSN, // The name of the dataset
BOOL bExclusive = FALSE, // If the dataset is to be exclusive
BOOL bReadOnly = FALSE, // If the dataset is read-only
LPCSTR lpszConnect = "ODBC;"); // The method of connection</FONT></PRE>
<P>The prototype of the MFC 3.0 (and later) versions of Open() function adds a new final parameter to the function:
<BR>
<PRE>
<FONT COLOR="#000080">BOOL Open( LPCSTR lpszDSN, // The name of the dataset
BOOL bExclusive = FALSE, // If the dataset is to be exclusive
BOOL bReadOnly = FALSE, // If the dataset is read-only
LPCSTR lpszConnect = "ODBC;", // The method of connection
BOOL bUseCursorLib = TRUE); // Use ODBC cursor library</FONT></PRE>
<P>The return value will be nonzero if the function is successful and zero if the user clicks the Cancel button in the Connection Information dialog box (if displayed). All other failures will cause the Open() function to throw an exception of type CDBException or<B> </B>CMemoryException.
<BR>
<P>The Close() function is used to close the connection that was established with the Open() function. The Close() function takes no parameters and has no return value. If no connection is currently open, this function does nothing. A call to Close() will cancel any pending AddNew() or Edit() statements and will roll back (discard) any pending transactions.
<BR>
<BR>
<A NAME="E70E5"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Database Attributes</B></FONT></CENTER></H5>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -