📄 ch14.htm
字号:
<TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Move </TD> <TD ALIGN="LEFT">Can be used to move a specific number of records from the current record or from the first record in the set. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SetAbsolutePosition </TD> <TD ALIGN="LEFT">Moves to the specified record in the set. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IsBOF </TD> <TD ALIGN="LEFT">Returns TRUE if the current record is the first record in the set. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IsEOF </TD> <TD ALIGN="LEFT">Returns TRUE if the current record is the last record in the set. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">GetRecordCount </TD> <TD ALIGN="LEFT">Returns the number of records in the set. </TD> </TR></TABLE></P><P>Of all of these navigation and informational functions, only two, Move and SetAbsolutePosition,take any arguments. The SetAbsolutePosition function takes a single numeric argumentto specify the row number of the record toward which to navigate. If you pass 0,it navigates to the beginning-of-file (BOF) position, whereas 1 takes you to thefirst record in the set. You can pass negative numbers to this function to causeit to count backward from the last record in the set. (For example, -1 takes youto the last record in the set, -2 to the next-to-last record, and so on.)</P><P>The Move function takes two arguments. The first argument is the number of rowsto move. This can be a positive or negative number; a negative number indicates abackward navigation through the record set. The second argument specifies how youwill move through the set of rows. The possible values for the second argument arelisted in Table 14.4 with descriptions of how they affect the navigation.</P><P><H4>TABLE 14.4. MOVE NAVIGATION 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">SQL_FETCH_RELATIVE </TD> <TD ALIGN="LEFT">Moves the specified number of rows from the current row. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SQL_FETCH_NEXT </TD> <TD ALIGN="LEFT">Moves to the next row, ignoring the number of rows specified. The same as calling the MoveNext function. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SQL_FETCH_PRIOR </TD> <TD ALIGN="LEFT">Moves to the previous row, ignoring the number of rows specified. The same as calling the MovePrev function. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SQL_FETCH_FIRST </TD> <TD ALIGN="LEFT">Moves to the first row, ignoring the number of rows specified. The same as calling the MoveFirst function. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SQL_FETCH_LAST </TD> <TD ALIGN="LEFT">Moves to the last row, ignoring the number of rows specified. The same as calling the MoveLast function. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SQL_FETCH_ABSOLUTE </TD> <TD ALIGN="LEFT">Moves the specified number of rows from the start of the set of rows. The same as calling the SetAbsolutePosition function. </TD> </TR></TABLE><H4>Adding, Deleting, and Updating Records</H4><P>Navigating a set of records from a database is only part of what you need to beable to do. You also need to be able to add new records to the record set, edit andupdate existing records, and delete records. These actions are all possible throughthe various functions that the CRecordset class provides. The functions that youwill use to provide this functionality to the user are listed in Table 14.5.</P><P><H4>TABLE 14.5. RECORD SET EDITING 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">AddNew </TD> <TD ALIGN="LEFT">Adds a new record to the record set. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Delete </TD> <TD ALIGN="LEFT">Deletes the current record from the record set. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Edit </TD> <TD ALIGN="LEFT">Allows the current record to be edited. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Update </TD> <TD ALIGN="LEFT">Saves the current changes to the database. </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Requery </TD> <TD ALIGN="LEFT">Reruns the current SQL query to refresh the record set. </TD> </TR></TABLE></P><P>None of these functions takes any arguments. However, some of them require followinga few specific steps to get them to work correctly.</P><P>To add a new record to the database, you can call the AddNew function. The nextthing that you need to do is set default values in any of the fields that requirevalues, such as the key fields. Next, you must call the Update function to add thenew record to the database. If you try to navigate to another record before callingthe Update function, the new record will be lost. Once you save the new record, youneed to call the Requery function to refresh the record set so that you can navigateto the new record and let the user edit it. This sequence of function calls typicallylooks like the following:</P><P><PRE>// Add a new record to the record setm_pSet.AddNew();// Set the key field on the new recordm_pSet.m_AddressID = m_lNewID;// Save the new record to the databasem_pSet.Update();// Refresh the record setm_pSet.Requery();// Move to the new recordm_pSet.MoveLast();</PRE><P>When you need to delete the current record, you can simply call the Delete function.Once you delete the current record, you need to navigate to another record so theuser isn't still looking at the record that was just deleted. Once you delete thecurrent record, there is no current record until you navigate to another one. Youdo not need to explicitly call the Update function because the navigation functionscall it for you. This allows you to write the following code to delete the currentrecord:</P><P><PRE>// Delete the current recordm_pSet.Delete();// Move to the previous recordm_pSet.MovePrev();</PRE><P>Finally, to allow the user to edit the current record, you need to call the Editfunction. This allows you to update the fields in the record with the new valuesentered by the user or calculated by your application. Once all changes are madeto the current record, you need to call the Update function to save the changes:</P><P><PRE>// Allow the user to edit the current recordm_pSet.Edit();// Perform all data exchange, updating the fields in the recordset..// Save the user's changes to the current recordm_pSet.Update();</PRE><P>You might be wondering how you get to the fields in the records to update them.When the AppWizard creates the CRecordset-derived class for your application, itadds all the fields in the records that will be in the record set as member variablesin order of the record set class. As a result, you can access the member variablesin order to access and manipulate the data elements in the database records thatare members of the record set.</P><P><H2><A NAME="Heading4"></A>Creating a Database Application Using ODBC</H2><P>For the sample application that you will build today, you'll create an SDI applicationwith ODBC database support. The application will retrieve records from an ODBC database,allowing the user to edit and update any of the records. You'll also add function-ality to enable the user to add new records to the database and to delete recordsfrom the database.</P><P><H3><A NAME="Heading5"></A>Preparing the Database</H3><P>Before you can begin building an application that uses a database, you need adatabase to use with your application. Almost every database that you can purchasefor your applications comes with tools for creating a new database. You'll need touse these tools to create your database and then use the ODBC administrator to configurean ODBC data source for your new database.</P><P>For the sample application in this chapter, I used Access 95 to create a new database.I used the Access Database Wizard to create the database, choosing the Address Bookdatabase template as the database to be created. When the Database Wizard started,I selected the default set of fields for including in the database and selected theoption to include sample data, as shown in Figure 14.1. I then accepted the restof the default settings offered in the Database Wizard.</P><P><A HREF="javascript:popUp('14fig01.gif')"><B>FIGURE 14.1.</B></A><B> </B><I>Includingsample data in the database.</I></P><P>Once you create the database, you need to configure an ODBC data source to pointto the database you just created. To do this, run the ODBC Administrator, which isin the Control Panel on your computer.</P><P>Once in the ODBC Administrator, you'll add a new data source. You can do thisby clicking the Add button, as shown in Figure 14.2. This opens another dialog, whichallows you to select the database driver for the new data source, as shown in Figure14.3. For the sample application that you will build today, because the databasewas created using Access, select the Microsoft Access Driver and click the Finishbutton.</P><P><A HREF="javascript:popUp('14fig03.gif')"><B>FIGURE 14.2.</B></A><B> </B><I>TheODBC Data Source Administrator.</I></P><P><A HREF="javascript:popUp('14fig04.gif')"><B>FIGURE 14.3.</B></A><B> </B><I>TheCreate New Data Source dialog.</I></P><P>In the ODBC Microsoft Access Setup dialog, shown in Figure 14.4, you'll providea short, simple name for the data source. Your application will use this name tospecify the ODBC data source configuration to use for the database connection, soit should reflect the function that the database will be serving, or it should besimilar to the name of the application that will be using this database. For thepurposes of the sample application database, name your data source <B>TYVCDB</B>(for Teach Yourself Visual C++ Database) and enter a description for the databasein the next field.</P><P>Once you enter a name and description for the data source, you need to specifywhere the database is. Click the Select button and then specify the Access databasethat you created. Once you finish configuring the ODBC data source for your database,click the OK button to add the new data source to the ODBC Administrator. You canclick the OK button to finish the task and close the ODBC Administrator because youare now ready to turn your attention to building your application.</P><P><A HREF="javascript:popUp('14fig05.gif')"><B>FIGURE 14.4.</B></A><B> </B><I>TheODBC Microsoft Access 97 Setup dialog.</I></P><P><I></I><H3><A NAME="Heading6"></A>Creating the Application Shell</H3><P>For the sample application that you will build today, you'll create a standardSDI-style application with database support. First, start a new project, selectingthe AppWizard, and give your application a suitable name, such as DbOdbc.</P><P>On the first AppWizard form, specify that you want to build an SDI application.On the second AppWizard form, specify that you want to include Database view withfile support. Click the Data Source button to specify which data source you willuse in your application. In the Database Options dialog, specify that you are usingan ODBC data source, and select the ODBC configuration from the list that you configuredfor your Access database, as shown in Figure 14.5. You can set the record set typeto either Snapshot or Dynaset.</P><P><A HREF="javascript:popUp('14fig06.gif')"><B>FIGURE 14.5.</B></A><B> </B><I>TheDatabase Options dialog.</I></P><P>Once you click the OK button, another dialog opens, presenting you with the availabletables in the database you selected. Select the Addresses table, as shown in Figure14.6, and click the OK button to close this dialog and return to the AppWizard.</P><P>You can continue through the rest of the AppWizard, accepting all of the defaultsettings. When you reach the final AppWizard step, you'll notice that the AppWizardis going to create an extra class. If you select this class, you'll see that it isderived from the CRecordset class, and it is the record set class for your application.You'll also notice that the view class is derived from the CRecordView class, whichis a descendent of the CFormView class, with some added support for database functionality.</P><P><A HREF="javascript:popUp('14fig07.gif')"><B>FIGURE 14.6.</B></A><B> </B><I>TheSelect Database Tables dialog.</I></P><P><I></I><H3><A NAME="Heading7"></A>Designing the Main Form</H3><P>Once you create the application shell, you need to design the main form that willbe used for viewing and editing the database records. You can design this form usingthe standard controls that are part of Visual C++, without adding any special ActiveXcontrols. For designing the main form in your sample application, lay out the mainform as shown in Figure 14.7, and configure the controls with the properties specifiedin Table 14.6.</P><BLOCKQUOTE> <P><HR><STRONG>TIP:</STRONG> If you want to save a little time when building the example, you can leave out most of the controls and database fields from the application. The key fields that you'll need to include are ID, First and Last Names, Birthdate, and Send Card. If you want to leave out the other fields from the application, that's fine.<HR></BLOCKQUOTE><H4>TABLE 14.6. CONTROL PROPERTY SETTINGS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Object</I></TD> <TD ALIGN="LEFT"><I>Property</I></TD> <TD ALIGN="LEFT"><I>Setting</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Static Text </TD> <TD ALIGN="LEFT">ID </TD> <TD ALIGN="LEFT">IDC_STATIC </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"> <P> </TD> <TD ALIGN="LEFT">Caption </TD> <TD ALIGN="LEFT">ID: </TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Edit Box </TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -