📄 ch15.htm
字号:
<P>You use the ADO_NUMERIC_ENTRY macros with numeric fields only. They are similar
to the ADO_FIXED_LENGTH_ENTRY macros in that there are two different versions of
the macro, named in the same way. In these macros, the first five parameters are
the same in both versions, along with the final parameter. Like with the ADO_FIXED_LENGTH_ENTRY
macros, the first version has an additional parameter that is not used in the second
version.</P>
<P>The first three parameters for the ADO_NUMERIC_ENTRY macros are the same as those
for the ADO_FIXED_LENGTH_ENTRY macros, as are the last parameter and the next to
last parameter for the first version. It is the fourth and fifth parameters that
are unique to these macros. The fourth parameter specifies the precision of the value
in 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 from
a 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 this
series of macros with database fields that are likely to vary in length. With a SQL-based
database, you want to use this series of macros with any varchar (variable-length
character 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 is
the parameters between them that vary.</P>
<P>The first parameter is the ordinal position of the column in the record set as
returned by the SQL query. The second parameter is the data type. The third parameter
is the variable in which the data value should be placed. The fourth parameter for
all versions of the macro is the size of the variable into which the value is to
be placed. This prevents the data from being written past the end of the variable
that you defined for it to be placed in. As with the previous macros, the final parameter
specifies whether the field is updateable.</P>
<P>In the first version of this macro, there are two parameters between the fourth
and final parameters. The second version of this macro only has the first of these
two 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. The
preceding 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 it
depends on which of the two methods you used to retrieve the data elements from the
recordset. 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 done
using the Recordset object's Update method, which takes two variables, the field
being updated and the new value for the field. You could make this update using the
following code:</P>
<P>
<PRE>_variant_t vName, vValue;
vName.SetString("FirstName");
vValue.SetString("John");
pRs->Update(vName, vValue);
</PRE>
<P>If you created your record class and bound it to the recordset, updating the record
is a little simpler. Once you have copied the new values into the variables in the
record class, you can call the record-bound version of the Update function, as in
the following:</P>
<P>
<PRE>picRs->Update(&m_rsRecSet);
</PRE>
<P>This updates the record in the Recordset object to be updated with the values
in 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 accomplish
it in other database access technologies. However, there are some slight subtleties
to 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 supposed
to be done. Most likely, you'll pass the adAffectCurrent value so that only the current
record in the recordset is deleted, as in the following code:</P>
<P>
<PRE>pRs->Delete(adAffectCurrent);
pRs->MovePrevious();
</PRE>
<P>As with any other database access technology, once you've deleted the current
record, there is no current record, so you need to navigate to another record before
allowing 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 record
set. If you check the variables in the record class that you created, you'll find
that they are all empty. However, you cannot just begin entering data values into
these fields. To allow the user to immediately enter the various data elements in
the new record, you'll blank out the values in the record class and pass this variable
as the only parameter to the Add New class. You need to call it through the record-binding
interface pointer, as in the following example:</P>
<P>
<PRE>CString strBlank = " ";
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->AddNew(&m_rsRecSet);
</PRE>
<P>This allows you to provide the user with a blank record, ready for editing. Once
the user has entered all the various values in the record, copy all these values
back 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 calling
the Close method, as follows:</P>
<P>
<PRE>pRs->Close();
</PRE>
<P>Once you finish all database interaction for the entire application, you'll also
close the connection to the database by calling the Connection object's Close method:</P>
<P>
<PRE>pConn->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 set
of records from an Access database, providing functionality to navigate the record
set. The user will be able to make changes to the data in the record set, and those
changes will be reflected in the database as well. The user will also be able to
add new records to the record set and delete records as desired. You will accomplish
all of this using ADO as the means of accessing the database, which will go through
the 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. As
with sev-eral other sample applications that you build in the course of reading this
book, everything that you do in today's application is just as applicable to an MDI
or dialog-style application. To start the application, you'll use the MFC AppWizard
to 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 project
something appropriate, such as DbAdo. Specify on the first panel of the AppWizard
that you are building an SDI-style application. Accept all the default settings for
steps 2 through 5, being sure to leave the second step stating that you want no database
support included in the application. On the final AppWizard step, specify that the
view class should be inherited from the CFormView class.</P>
<P>Once you finish creating your application shell, design the main dialog form for
use in your application. Add the standard controls for each of the fields in the
Addresses table from the database you used yesterday (or if you used a different
database 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 Table
15.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>The
main 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -