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

📄 ch15.htm

📁 这是一本关于VC++自学教程的书籍 对于初学者以及编程有一定基础的人均有帮助
💻 HTM
📖 第 1 页 / 共 3 页
字号:
last parameter for the first version. It is the fourth and fifth parameters thatare unique to these macros. The fourth parameter specifies the precision of the valuein this field of the record set. The fifth parameter specifies the scale of the value.Both of these parameters are crucial in correctly converting the value to and froma variant data type.</P><P><H4>The ADO_VARIABLE_LENGTH_ENTRY Macros</H4><P>The final series of macros is the ADO_VARIABLE_LENGTH_ENTRY macros. You use thisseries of macros with database fields that are likely to vary in length. With a SQL-baseddatabase, you want to use this series of macros with any varchar (variable-lengthcharacter string) columns. There are three versions of this macro. In all three versions,the first four parameters are the same, and the final parameter is the same. It isthe parameters between them that vary.</P><P>The first parameter is the ordinal position of the column in the record set asreturned by the SQL query. The second parameter is the data type. The third parameteris the variable in which the data value should be placed. The fourth parameter forall versions of the macro is the size of the variable into which the value is tobe placed. This prevents the data from being written past the end of the variablethat you defined for it to be placed in. As with the previous macros, the final parameterspecifies whether the field is updateable.</P><P>In the first version of this macro, there are two parameters between the fourthand final parameters. The second version of this macro only has the first of thesetwo parameters, and the third version only has the second of these two parameters.The first of these two parameters is the status variable for use with this field.The second of these two parameters is the length of the field in the database. Thepreceding example used the second version of this macro.</P><P><H3><A NAME="Heading10"></A>Updating Records</H3><P>When you need to update values in a record in the recordset, how you handle itdepends on which of the two methods you used to retrieve the data elements from therecordset. If you retrieved each field and converted it from a variant yourself,you need to update each individual field that has been changed. The update is doneusing the Recordset object's Update method, which takes two variables, the fieldbeing updated and the new value for the field. You could make this update using thefollowing code:</P><P><PRE>_variant_t vName, vValue;vName.SetString(&quot;FirstName&quot;);vValue.SetString(&quot;John&quot;);pRs-&gt;Update(vName, vValue);</PRE><P>If you created your record class and bound it to the recordset, updating the recordis a little simpler. Once you have copied the new values into the variables in therecord class, you can call the record-bound version of the Update function, as inthe following:</P><P><PRE>picRs-&gt;Update(&amp;m_rsRecSet);</PRE><P>This updates the record in the Recordset object to be updated with the valuesin the record class that you have bound to the set.</P><P><H3><A NAME="Heading11"></A>Adding and Deleting</H3><P>Adding and deleting records from an ADO recordset is similar to how you accomplishit in other database access technologies. However, there are some slight subtletiesto how you perform the addition of new records.</P><P>For deleting the current record, you can call the Recordset object's Delete method.This method requires a single parameter that specifies how the delete is supposedto be done. Most likely, you'll pass the adAffectCurrent value so that only the currentrecord in the recordset is deleted, as in the following code:</P><P><PRE>pRs-&gt;Delete(adAffectCurrent);pRs-&gt;MovePrevious();</PRE><P>As with any other database access technology, once you've deleted the currentrecord, there is no current record, so you need to navigate to another record beforeallowing the user to do anything else.</P><P>When you are adding a new record, you can call the Recordset object's AddNew method.Once you have added a new record, the new record is the current record in the recordset. If you check the variables in the record class that you created, you'll findthat they are all empty. However, you cannot just begin entering data values intothese fields. To allow the user to immediately enter the various data elements inthe new record, you'll blank out the values in the record class and pass this variableas the only parameter to the Add New class. You need to call it through the record-bindinginterface pointer, as in the following example:</P><P><PRE>CString strBlank = &quot; &quot;;COleDateTime dtBlank;m_rsRecSet.m_lAddressID = 0;strcpy(m_rsRecSet.m_szFirstName, (LPCTSTR)strBlank);m_rsRecSet.m_dtBirthdate = (DATE)dtBlank;m_rsRecSet.m_bSendCard = VARIANT_FALSE;picRs-&gt;AddNew(&amp;m_rsRecSet);</PRE><P>This allows you to provide the user with a blank record, ready for editing. Oncethe user has entered all the various values in the record, copy all these valuesback to the record variable. Then, call the Update method to save the record.</P><P><H3><A NAME="Heading12"></A>Closing the Recordset and Connection Objects</H3><P>Once you finish working with a record set, you'll close the record set by callingthe Close method, as follows:</P><P><PRE>pRs-&gt;Close();</PRE><P>Once you finish all database interaction for the entire application, you'll alsoclose the connection to the database by calling the Connection object's Close method:</P><P><PRE>pConn-&gt;Close();</PRE><H2><A NAME="Heading13"></A>Building a Database Application Using ADO</H2><P>The sample application that you will build today is another simple database application,basically the same as the one you built yesterday. You'll use ADO to retrieve a setof records from an Access database, providing functionality to navigate the recordset. The user will be able to make changes to the data in the record set, and thosechanges will be reflected in the database as well. The user will also be able toadd new records to the record set and delete records as desired. You will accomplishall of this using ADO as the means of accessing the database, which will go throughthe ODBC driver that was configured yesterday.</P><P><H3><A NAME="Heading14"></A>Creating the Application Shell</H3><P>The application that you will build today will be an SDI-style application. Aswith sev-eral other sample applications that you build in the course of reading thisbook, everything that you do in today's application is just as applicable to an MDIor dialog-style application. To start the application, you'll use the MFC AppWizardto build the application shell, using most of the SDI-style application default settings.</P><P>To start your application, create a new AppWizard project, naming the projectsomething appropriate, such as DbAdo. Specify on the first panel of the AppWizardthat you are building an SDI-style application. Accept all the default settings forsteps 2 through 5, being sure to leave the second step stating that you want no databasesupport included in the application. On the final AppWizard step, specify that theview class should be inherited from the CFormView class.</P><P>Once you finish creating your application shell, design the main dialog form foruse in your application. Add the standard controls for each of the fields in theAddresses table from the database you used yesterday (or if you used a differentdatabase yesterday, add controls for all the fields in the table that you used),as shown in Figure 15.6. Configure the controls using the properties listed in Table15.1.</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 on the screen 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. You will need to include these fields in the CCustomRs class that you create	in this chapter.<HR></BLOCKQUOTE><P><A HREF="javascript:popUp('15fig06.gif')"><B>FIGURE 15.6.</B></A><B> </B><I>Themain form layout.</I></P><P><I></I><H4>TABLE 15.1. 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">Address ID		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_ADDRESSID		</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">First Name		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_FIRSTNAME		</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">Last Name		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_LASTNAME		</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">Spouse Name		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_SPOUSENAME		</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">Address		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_ADDRESS		</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">City		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_CITY		</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">State Or Province		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_STATEORPROVINCE		</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">Postal Code		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EDIT_POSTALCODE		</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">Country		</TD>	</TR>	<TR ALIG

⌨️ 快捷键说明

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