- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Using the AppWizard to Generate MFC Classes
- The CRecordset Class
- The CRecordView Class
- The CDatabase Class
- Exception Handling
- Bulk Row Operations
- Summary
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
Using the AppWizard to Generate MFC Classes
Before delving into the specifics of MFC ODBC database classes, this section shows seven simple steps you can use to quickly and easily generate an ODBC database application using the MFC AppWizard.
Step 1
Start a new Visual C++ project by choosing File, New. Choose MFC AppWizard (exe), enter a project name, and click OK. The MFC AppWizard appears. In Step 1 of the AppWizard, choose Single Document and click Next.
Step 2
In Step 2 of the AppWizard, you are asked what type of database support you would like. You have four options:
- None— Gives no database support. This is the default.
- Header Files Only— Includes only the header files for the MFC database classes and adds build settings to link with the MFC database libraries. It does not define any database classes for you.
- Database View without File Support— Generates database classes for you to modify to make a customized database application.
- Database View with File Support— Generates database classes for you and provides support for document serialization and the File menu options associated with document files, such as the Open, Close, Save, and Save As File menu options.
You must choose a datasource to be used with these classes by clicking the Data Source button. A dialog box appears; choose a datasource and set the type of recordset to use. You also are prompted for specific tables, views, and stored procedures in the datasource to use for the recordset and record view classes.
Select the Database View with File Support option, and click the Data Source button.
Step 3
The Database Options dialog box now should be open. Here, you choose what type of database support (ODBC, OLE DB, or DAO) you want to include in your application, the name of the datasource, and the recordset type. Possible ODBC recordset types follow:
- Snapshot— "Takes a picture" of the results of a query or SELECT statement when the class is first called. Any changes to the database made while your application is running are not reflected in your application.
- Dynaset— Constantly resynchronizes the recordset so that any updates or deletions of other users (or other applications) are reflected in your dataset.
In Figure 21.1, I selected an ODBC database and chose a Dynaset Recordset type.
Figure 21.1 With the MFC AppWizard, you can create database applications with a variety of datasource types and recordset types.
Step 4
After you choose a valid ODBC datasource, the AppWizard displays a list of tables inside that datasource. Here, you can choose one or more tables or views (or, in MS Access, queries) that you want to use in your database application. In Figure 21.2, the Employee table is chosen as the source for the data in this application.
Figure 21.2 You must choose the table or tables that will serve as datasources for your AppWizard application.
Step 5
Click Next twice until the MFC AppWizard (Step 4 of 6 dialog box) opens. Deselect Printing and Print Preview. Click Finish. If you've been following this example, a New Project Information dialog box opens with information similar to the information shown in Figure 21.3. Click OK to generate your application.
Figure 21.3 The New Project Information dialog box displays the options you chose when developing your MFC AppWizard application.
Step 6
Now that you have an application with database support, you need to display that information. In Figure 21.4, you see a sample dialog box that was created with two edit boxes to hold information from the Employee table. These two dialog boxes are named IDC_EMPNAME and IDC_DEPTCODE and hold the employee name and department code.
Figure 21.4 You need to build dialog box fields to hold the results from your database application.
Step 7
In this final step, you need to use the ClassWizard to link your database variables to your dialog box edit boxes. First, open the ClassWizard by choosing View, ClassWizard. Then select the Member Variables tab, as shown in Figure 21.5. The edit boxes you added to your view's dialog box should be displayed.
Figure 21.5 The ClassWizard Member Variables tab shows the controls added to your view's dialog box.
Double-click each variable. Alternatively, highlight each variable and click the Add Variable button. The Add Member Variable dialog box opens, as shown in Figure 21.6. Here, choose the appropriate database variable from the drop-down Member Variable Name list and click OK.
Figure 21.6 Adding database member variables to the dialog box edit boxes.
The ClassWizard Member Variables tab appears (see Figure 21.7).
Figure 21.7 The ClassWizard displays the database variables linked to the dialog box edit boxes.
Now you're finished. When you build your application, you can see a database application that queries the database and allows updates to the data, as shown in Figure 21.8.
Figure 21.8 Simple database applications are easy to build using the MFC AppWizard.
Although the MFC AppWizard is a great tool for quick ODBC database development, eventually database developers will need to make modifications to their database applications. These modifications require an understanding of the MFC ODBC classes. The rest of this chapter describes the MFC ODBC classes that were generated by the MFC AppWizard.
The CRecordset Class | Next Section

Account Sign In
View your cart