⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch14.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD>	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css">	<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">	<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 14 -- Retrieving Data from an ODBC Database8</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR>Teach Yourself Visual C++ 6 in 21 Days</H1><CENTER><P><A HREF="../ch13/ch13.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch15/ch15.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER">- 14 -<BR>Retrieving Data from an ODBC Database</H1><H1></H1><UL>	<LI><A HREF="#Heading1">Database Access and ODBC</A>	<UL>		<LI><A HREF="#Heading2">The Open Database Connector (ODBC) Interface</A>		<LI><A HREF="#Heading3">The CRecordset Class</A>	</UL>	<LI><A HREF="#Heading4">Creating a Database Application Using ODBC</A>	<UL>		<LI><A HREF="#Heading5">Preparing the Database</A>		<LI><A HREF="#Heading6">Creating the Application Shell</A>		<LI><A HREF="#Heading7">Designing the Main Form</A>		<LI><A HREF="#Heading8">Adding New Records</A>		<LI><A HREF="#Heading9">Deleting Records</A>	</UL>	<LI><A HREF="#Heading10">Summary</A>	<LI><A HREF="#Heading11">Q&amp;A</A>	<LI><A HREF="#Heading12">Workshop</A>	<UL>		<LI><A HREF="#Heading13">Quiz</A>		<LI><A HREF="#Heading14">Exercise</A>	</UL>	<LI><A HREF="#Heading15">In Review</A></UL><P><HR SIZE="4"><BR>A large number of applications use a database. Everything from a personal organizerto a large, corporate personnel system uses a database to store and maintain allthe records that the applications use and manipulate. Visual C++ provides you withfour different technologies for using and accessing databases in your applications,Data Access Objects (DAO), ODBC, OLE DB, and ActiveX Data Objects (ADO). Today andtomorrow, you'll learn about two of these technologies, how they differ, and howyou can use them in your own applications. Today, you will learn</P><P><UL>	<LI>How the ODBC interface allows you to use a consistent way to access a database.	<P>	<LI>How Visual C++ uses the CRecordset class to provide access to an ODBC data source.	<P>	<LI>How you can create a simple database application using the Wizards in Visual	C++.	<P>	<LI>How you can add and delete records from an ODBC database in Visual C++.</UL><H2><A NAME="Heading1"></A>Database Access and ODBC</H2><P>Most business applications work with data. They maintain, manipulate, and accessrecords of data that are stored in databases. If you build business applications,odds are that you will need to be able to access a database with your applications.The question is, which database?</P><P>There are a number of databases on the market. If you need to create a single-userapplication that is self-contained on a single computer, you can use any one of numerousPC-based databases, such as Microsoft's Access, FoxPro, or Borland's Paradox. Ifyou are building applications that need to access large, shared databases, you areprobably using an SQL-based (Structured Query Language) database such as SQL Serveror Oracle. All of these databases provide the same basic functionality, maintainingrecords of data. Each will allow you to retrieve several records or a single record,depending on your needs. They'll all let you add, update, or delete records as needed.Any of these data- bases will be able to serve your application's needs, so you shouldbe able to use any database for one application and then switch to another for thenext application, based on the needs of the application and which database is mostsuited for the specific application needs (or your employer's whim).</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> To be completely honest, there are numerous differences between the	various databases that are available today. Each of these databases has specific	strengths and weaknesses, making one more suitable for a specific situation than	another. However, a discussion of the differences between any of these databases	is beyond the scope of this book. For the discussions of databases today and tomorrow,	you can assume that all of these databases are functionally equal and interchangeable.<HR></BLOCKQUOTE><P>The problem that you will encounter when you switch from one database to anotheris that each database requires you to use a different interface for accessing thedatabase. Therefore, you have to learn and use a whole new set of programming techniquesand functions for each database that you need to work with. This is the problem thatthe ODBC interface was designed to correct.</P><P><H3><A NAME="Heading2"></A>The Open Database Connector (ODBC) Interface</H3><P>Microsoft saw the incompatibility between database interfaces as a problem. Eachdatabase had its own application development language that was well integrated withthe database but didn't work with any other database. This presented a problem toany developer who needed to use one database for an application and then a differentdatabase for the next application. The developer had to learn the specific developmentlanguage for each of the databases and couldn't use any languages that she alreadyknew. For programmers to work with any database with the programming language ofthe developer's choice, they needed a standardized interface that works with everydatabase.</P><P>The Open Database Connector (ODBC) interface is implemented as a standard, SQL-basedinterface that is an integral part of the Windows operating system. Behind this interfaceare plug-ins for each database that take the ODBC function calls and convert theminto calls to the specific interface for that database. The ODBC interface also usesa central set of database connection configurations, with a standardized way of specifyingand maintaining them. This setup allows programmers to learn and use a single databaseinterface for all databases. This also allowed programming language vendors to addODBC support into their languages and development tools to make database access allbut transparent.</P><P><H3><A NAME="Heading3"></A>The CRecordset Class</H3><P>In the Visual C++ development environment, most of the ODBC functionality hasbeen encapsulated into two classes, CRecordset and CDatabase. The CDatabase classcontains the database connection information and can be shared across an entire application.The CRecordset class encapsulates a set of records from the database. The CRecordsetclass allows you to specify a SQL query to be run, and the CRecordset class willrun the query and maintain the set of records that are returned by the database.You can modify and update the records in the record set, and your changes will bepassed back to the database. You can add or delete records from the record set, andthose same actions can be passed back to the database.</P><P><H4>Connecting to the Database</H4><P>Before the CRecordset class can perform any other functions, it has to be connectedto a database. This is accomplished through the use of the CDatabase class. You don'tneed to create or set the CDatabase instance; the first instance of the CRecordsetclass does this for you. When you create an application using the AppWizard and chooseto include ODBC database support, the AppWizard includes the database connectioninformation in the first CRecordset-derived class that it creates. When this CRecordsetclass is created without being passed a CDatabase object, it uses the default connectioninformation, which was added by the AppWizard, to create its own database connection.</P><P><H4>Opening and Closing the Record Set</H4><P>Once the CRecordset object is created and connected to the database, you needto open the record set to retrieve the set of records from the database. Do thisby calling the Open member function of the CRecordset object. You can call this functionwithout any arguments if you want to take the default values for everything, includingthe SQL statement to be executed.</P><P>The first argument to the Open function is the record set type. The default valuefor this, AFX_DB_USE_DEFAULT_TYPE, is to open the record set as a snapshot set ofrecords. Table 14.1 lists the four types of record set types. Only two of these recordset types are available in the AppWizard when you are specifying the data source.</P><P><H4>TABLE 14.1. RECORD SET TYPES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Type</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::dynaset		</TD>		<TD ALIGN="LEFT">A set of records that can be refreshed by calling the Fetch function so that changes			made to the record set by other users can be seen.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::snapshot		</TD>		<TD ALIGN="LEFT">A set of records that cannot be refreshed without closing and then reopening the			record set.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::dynamic		</TD>		<TD ALIGN="LEFT">Very similar to the CRecordset::dynaset type, but it is not available in many ODBC			drivers.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::forwardOnly		</TD>		<TD ALIGN="LEFT">A read-only set of records that can only be scrolled from the first to the last record.		</TD>	</TR></TABLE></P><P>The second argument to the Open function is the SQL statement that is to be executedto populate the record set. If a NULL is passed for this argument, the default SQLstatement that was created by the AppWizard is executed.</P><P>The third argument is a set of flags that you can use to specify how the set ofrecords is to be retrieved into the record set. Most of these flags require an in-depthunderstanding of the ODBC interface so you understand how the flags can and shouldbe used in your applications. Because of this, I'll discuss only a few of these flagsin Table 14.2.</P><P><H4>TABLE 14.2. RECORD SET OPEN FLAGS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Flag</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::none		</TD>		<TD ALIGN="LEFT">The default value for this argument; specifies that no options affect how the record			set is opened and used.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::appendOnly		</TD>		<TD ALIGN="LEFT">This flag prevents the user from being able to edit or delete any of the existing			records in the record set. The user will only be able to add new records to the set			of records. You cannot use this option with the CRecordset::readOnly flag.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CRecordset::readOnly		</TD>		<TD ALIGN="LEFT">This flag specifies that the record set is read-only and no changes can be made by			the user. You cannot use this option with the CRecordset::appendOnly flag.		</TD>	</TR></TABLE></P><P>Once the user finishes working with the record set, you can call the Close functionto close the record set and free any resources used by the record set. The Closefunction doesn't take any arguments.</P><P><H4>Navigating the Record Set</H4><P>Once you have a set of records retrieved from the database, you need to be ableto navigate the set of records (unless the set has only one record). The CRecordsetclass provides several functions for navigating the record set, allowing you to movethe user to any record. Table 14.3 lists the functions that you use to navigate therecord set.</P><P><H4>TABLE 14.3. RECORD SET NAVIGATION FUNCTIONS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Function</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">MoveFirst		</TD>		<TD ALIGN="LEFT">Moves to the first record in the set.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">MoveLast		</TD>		<TD ALIGN="LEFT">Moves to the last record in the set.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">MoveNext		</TD>		<TD ALIGN="LEFT">Moves to the next record in the set.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">MovePrev		</TD>		<TD ALIGN="LEFT">Moves to the previous record in the set.		</TD>	</TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -