Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

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:

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:

In Figure 21.1, I selected an ODBC database and chose a Dynaset Recordset type.

21fig01.gif

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.

21fig02.gif

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.

21fig03.gif

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.

21fig04.gif

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.

21fig05.gif

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.

21fig06.gif

Figure 21.6 Adding database member variables to the dialog box edit boxes.

The ClassWizard Member Variables tab appears (see Figure 21.7).

21fig07.gif

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.

21fig08.gif

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.

Share ThisShare This

Informit Network