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

📄 ch14.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<PRE>1:  void CDbOdbcView::OnRecordNew() 2:  {3:      // TODO: Add your command handler code here4:      // Get a pointer to the record set5:      CRecordset* pSet = OnGetRecordset();6:      // Make sure that any changes to the current record7:      // have been saved8:      if (pSet-&gt;CanUpdate() &amp;&amp; !pSet-&gt;IsDeleted())9:      {10:         pSet-&gt;Edit();11:         if (!UpdateData())12:             return;13:14:         pSet-&gt;Update();15:     }16:     // Get the ID for the new record17:     long m_lNewID = m_pSet-&gt;GetMaxID() + 1;18:     // Add the new record19:     m_pSet-&gt;AddNew();20:     // Set the ID in the new record21:     m_pSet-&gt;m_AddressID = m_lNewID;22:     // Save the new record23:     m_pSet-&gt;Update();24:     // Refresh the record set25:     m_pSet-&gt;Requery();26:     // Move to the new record27:     m_pSet-&gt;MoveLast();28:     // Update the form29:     UpdateData(FALSE);30: }</PRE><P>Add a new toolbar button for the New Record menu, and then compile and run yourapplication. You should be able to add new records to the database, entering thedata you want into the records.</P><P><H3><A NAME="Heading9"></A>Deleting Records</H3><P>The only functionality remaining is the ability to delete the current record fromthe database. You'll need to add another menu entry to trigger this action. Oncethe action is triggered, you'll verify that the user really does want to delete thecurrent record and then call the Delete function to remove the record. Once the recordhas been deleted, you'll call the MovePrev function to navigate to the previous recordin the set.</P><P>To add this functionality to your application, you'll need a menu entry that theuser can select to delete the current record from the database. Add a new menu entryto the Record menu. Configure the new menu entry with the properties in Table 14.9.</P><P><H4>TABLE 14.9. MENU 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">			<PRE>Menu Entry</PRE>		</TD>		<TD ALIGN="LEFT">			<PRE>ID</PRE>		</TD>		<TD ALIGN="LEFT">			<PRE>IDM_RECORD_DELETE</PRE>		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Delete Record		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Prompt		</TD>		<TD ALIGN="LEFT">Delete the current record\nDelete Record		</TD>	</TR></TABLE></P><P>Using the Class Wizard, add an event-handler function for the COMMAND event messagefor this menu to the view class, CDbOdbcView. Edit this function, adding the codein Listing 14.6.</P><P><H4>LISTING 14.6. THE CDbOdbcView OnRecordDelete FUNCTION.</H4><PRE>1:  void CTestdb5View::OnRecordDelete() 2:  {3:      // TODO: Add your command handler code here4:      // Make sure the user wants to delete this record5:      if (MessageBox(&quot;Are you sure you want to delete this record?&quot;,6:              &quot;Delete this record?&quot;, MB_YESNO | MB_ICONQUESTION) ==                   &Acirc;IDYES)7:      {8:          // Delete the record9:          m_pSet-&gt;Delete();10:         // Move to the previous record11:         m_pSet-&gt;MovePrev();12:         // Update the form13:         UpdateData(FALSE);14:     }15: }</PRE><P>Add another button to the toolbar and associate it with the IDM_RECORD_DELETEmenu ID so that the user can delete the current record without having to go to themenu. If you compile and run your application at this point, you'll have a full-functiondatabase application in which you can add, edit, and delete records, as shown inFigure 14.10.</P><P><A HREF="javascript:popUp('14fig11.gif')"><B>FIGURE 14.10.</B></A><B> </B><I>Thecompleted application.</I></P><P><I></I><H2><A NAME="Heading10"></A>Summary</H2><P>Today, you learned how you can use the ODBC interface to build database applicationsthat can be easily run against any database you might need to use. You saw how theCRecordset class provides you with a substantial amount of functionality so thatyou can provide database functionality in your applications. You also saw how theAppWizard provides you with a large amount of database functionality without yourtyping a single line of code.</P><P>Tomorrow, you will learn about Microsoft's newest database access technology,ActiveX Data Objects, and how this can be combined with the ODBC interface to makeyour database access even easier.</P><P><H2><A NAME="Heading11"></A>Q&amp;A</H2><DL>	<DT></DT>	<DD><B>Q Why would I want to use the ODBC interface instead of the Data Access Objects?</B>	<P>	<DT><B></B></DT>	<DD><B>A</B> The Data Access Objects (DAO) use the Microsoft Jet database engine	to perform all of the database access. This adds at least a megabyte of overhead	to your application, and if you're using a SQL-based database, the database is already	doing all of the work that the Jet engine is doing for you. What's more, the Jet	database engine uses the ODBC interface to access any SQL-based databases. As a result,	unless you are using PC-based databases, such as Access, FoxPro, or Paradox, you	get better performance from going directly to the ODBC interface yourself.	<P>	<DT></DT>	<DD><B>Q How can I add different record sets in an MDI application?</B>	<P>	<DT><B></B></DT>	<DD><B>A</B> You can add additional CRecordset-derived classes through the New Class	Wizard in an MDI application project. You need to specify that the new class is an	MFC class and that its base class is the CRecordset class. The New Class Wizard will	have you specify the data source, just as the AppWizard had you do when creating	the shell for today's application. Once you create the record set class, you can	create a new view class the same way, specifying the base class as CRecordView. Once	you click the OK button, the New Class Wizard asks you to specify which of the record	set classes to use with the new record view class.	<P></DL><H2><A NAME="Heading12"></A>Workshop</H2><P>The Workshop provides quiz questions to help you solidify your understanding ofthe material covered and exercises to provide you with experience in using what you'velearned. The answers to the quiz questions and exercises are provided in AppendixB, &quot;Answers.&quot;</P><P><H3><A NAME="Heading13"></A>Quiz</H3><DL>	<DT></DT>	<DD><B>1. </B>What does ODBC stand for?	<P>	<DT></DT>	<DD><B>2. </B>What functions can you use to navigate the record set in a CRecordset	object?	<P>	<DT></DT>	<DD><B>3. </B>What view class should you use with an ODBC application?	<P>	<DT></DT>	<DD><B>4. </B>What sequence of functions do you need to call to add a new record	to a record set?	<P>	<DT></DT>	<DD><B>5. </B>What function do you need to call before the fields in the CRecordset	object can be updated with any changes?	<P></DL><H3><A NAME="Heading14"></A>Exercise</H3><P>Add a menu entry and dialog to let the user indicate the record number to moveto, and then move to that record.</P><P><H2><A NAME="Heading15"></A>In Review</H2><P>Now that you've finished the second week, you should be getting very comfortableworking with Visual C++. You should be beginning to understand how you can use theMFC class hierarchy to provide a substantial amount of existing functionality inyour applications. You should also be starting to understand how much supportinginfrastructure your applications start with when you use the Visual C++ wizards toconstruct as much of your application as you can.</P><P>This is a good time to take a little break and try some of the things that you'velearned on your own. Build an MDI application, using a custom document type thatyou've come up with yourself. See how you can save and restore the document, as wellas maintain it. Practicing on your own is key to cementing your understanding ofwhat you've learned in this book. This will help you identify any areas that youmight need to go back and read again, as well as those areas where you feel comfortableenough to not review.</P><P>By this time, you should have a good understanding of the Document/View architectureand how it can be used to maintain the separation of the data from the representationof the data that is displayed for the user. You've used this model for both SingleDocument Interface (SDI) and Multiple Document Interface (MDI) style applications,and you've used it for reading and writing files to the disk drive. This model isone of the main building blocks of MFC applications built with Visual C++. You shouldknow where to place any initialization information for a new set of data and whereto clean up when closing a set of data.</P><P>You should also have a good understanding of how the SDI and MDI application stylesare alike and how they differ from each other and from the dialog application style.You should have a good idea of when an application you are building should use oneof these styles and when it should use a different style. You should be able to createyour own SDI and MDI applications, as you need to, without any significant problems.If you've got any questions about either of these areas, you might want to take anotherlook at Days 10 and 11 to review how the Document/View architecture works in bothSDI and MDI style applications.</P><P>You should understand how, in SDI and MDI style applications, you can save andrestore complex data structures in files on the system hard drive. You should beable to create mixed-type objects that you create and maintain in the document objectin your applications, be able to use the Serialize function with the CArchive objectto write the objects to a file, and then be able to restore the objects at a latertime. If you are having any trouble understanding how this works or are running intoany problems trying to implement this functionality in your own applications, reviewDay 13.</P><P>Along with reading and writing files, you also have learned how you can designand build toolbars for use in your SDI and MDI applications. At this point, you shouldbe completely comfortable with designing and creating your own toolbars and usingthem in your applications. You should understand the importance of matching the toolbarbutton ID to the ID of the menu for which the toolbar will be used as a substitute.You should also have a basic understanding of creating and using your own customizedstatus bar elements in SDI and MDI applications. You should understand how you canuse the UPDATE_COMMAND_UI event message to evaluate and alter the status of menu,toolbar, and status bar elements, relieving you of all the work of setting each ofthese elements, and how to maintain their appearance and status yourself. If youaren't clear on how you can do any of these things, you might want to go back overDay 12 one more time.</P><P>You've seen how you can build a simple database application, pulling data froma database through the ODBC interface. You should have a basic understanding of howyou can build database applications using this approach, how to maintain the data,how to add new records, and how to delete records. You should know how all the databaseinteraction is directed through the record set class and how you can directly controlthe data through this object. If you're not sure of some of this, you might wantto look back at Day 14 for a quick refresher.</P><P>You learned how easy it is to add ActiveX controls to your projects and how VisualC++ builds C++ classes around the control, enabling you to interact with the controlas if it were just another C++ object. You should have a good grasp of how to addany ActiveX control (armed with the documentation for the control) to your applicationand interact with it in a seamless manner. You should be able to declare a variablefor the control, set the control's properties, call its methods, and react to itsevents just as if it were a standard part of the Visual C++ development environment.If you aren't sure how you can do some of this, you might want to go back and rereadDay 9.</P><P>Finally, you started this week by learning how to draw graphics on the windowsof your applications. You learned how to draw lines, circles, and squares, usinga variety of pens and brushes. You even learned how you can make a customized brushfrom a bitmap. You learned how you can load a bitmap image from a file and displayit for the user to see. But most importantly, you learned about the device contextand how it is used to draw all these features on the windows of your applications.You should be able to use these and other figure drawing device context methods todraw any image you might want to draw on the window for the user to see and interactwith. If you are unsure about how you can do this, you probably want to look backat Day 8 once more.</P><P>By this time, you have built up quite a set of programming skills with VisualC++. You are probably ready to tackle most of the smaller programming tasks you mightencounter--and maybe even a few not-so-small tasks. At this point, you are well onyour way to becoming an accomplished Visual C++ programmer. That said--now is notthe time to stop because there's still more to be learned. There's only one moreweek to go, so tallyho!</P><P><H1></H1><CENTER><P><HR><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> <BR><BR></P><P>&copy; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>

⌨️ 快捷键说明

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