ClassWizard: Creating a Recordset Class
This article explains how to create a recordset class with ClassWizard.
You'll need a new -derived class (for ODBC) or -derived class (for DAO) for each table, join of tables, or predefined query you work with in your program. Each recordset class specifies its own set of columns and may also specify parameters. For information about the structure of your recordset class and its uses, see the article Recordset: Architecture (ODBC) or DAO Recordset: Architecture.
For information about mapping recordset field data members to columns in the table, see the article ClassWizard: Binding Recordset Fields to Table Columns.
For information about using a (ODBC) for a join of tables, see the article Recordset: Performing a Join (ODBC). (Performing a join with DAO is similar.)
For information about using a CRecordset for a predefined query, see the article Recordset: Declaring a Class for a Predefined Query (ODBC). For information about predefined ("saved") queries in DAO, see the article DAO Querydef.
Note When creating CRecordset-derived or CDaoRecordset-derived classes with ClassWizard, be careful when selecting multiple tables or queries. Selecting multiple tables or queries will result in the construction of a JOIN query without any restriction on how to perform the JOIN (called a cross product or a Cartesian product JOIN). You may wish to specify a filter using or (resulting in MFC building an SQL WHERE clause) before the recordset is opened. This will constrain the number of records in the result set. This is especially necessary when using the ODBC Cursor Library, since the Cursor Library may create a large temporary file for result sets with many records.
The table below shows the major steps in creating a recordset class.
Recordset Class Creation Summary
To | Do this |
Create the class | Use the Add Class dialog box in ClassWizard. |
Select a data source and database table for the class | Select options in the Database Options dialog box. These include a data source, possibly a recordset type, and possibly some advanced options. Then specify details about the data source in the Select Database dialog box (for ODBC) or the Open dialog box (for DAO). Next, use the Select Database Tables dialog box to select tables from those available on the data source. |
Remove any column mappings you don't want. By default, AppWizard and ClassWizard bind all columns in the table to recordset field data members | Select a column name on the Member Variables tab and choose Delete Variable. |
Optionally parameterize the underlying SQL statements | Manually add parameter data members, or, in DAO, base your recordset on a parameterized a querydef object. |
Optionally use dialog data exchange (DDX) to map recordset data members to controls in a record view | See the article Record Views. |
For more information about parameterizing your class, see the article Recordset: Parameterizing a Recordset (ODBC) or the article DAO Queries: Filtering and Parameterizing Queries. For information about using DDX between record view controls and recordset data members, see the article ClassWizard: Mapping Form Controls to Recordset Fields.
To create the recordset class
You can add a new class derived from class or in ClassWizard's Add Class dialog box. This dialog box is available from any tab in ClassWizard. (All figures follow the procedure below.)
On the View menu, click ClassWizard.
Click Add Class and select New from the drop-down list.
The Add Class dialog box appears (see figure below).
In the Name box, enter a name for the class; in the Filenames box, enter filenames for its .h and .cpp files.
From the Base class drop-down list, select as the base-class type of the new class. If you're using the MFC DAO classes, select .
Click OK.
The Database Options dialog box appears (see figure below).
Choose ODBC or DAO. Then select a data source:
For ODBC, select from the drop-down list. Depending on what you choose, you might need to make a further selection in an Open dialog box. If the data source is on a server, you might be prompted to log in to the server.
For DAO, click the browse button beside the DAO edit control. Then, in the Open dialog box, navigate to the database file you want to use. (By default, the dialog box displays only Microsoft Jet databases, .mdb, but you can open any database that the Microsoft Jet database engine can read. For information, see the article Databases: Overview.
Select the Recordset type (Snapshot, Dynaset, or Table) and Advanced features you want the class to have.
Click OK.
In the Select Database Tables dialog box (see figure below), select the name(s) of the table(s) you want.
Click OK.
On ClassWizard's Member Variables tab, notice that ClassWizard binds all of the table's columns to recordset field data members. For information about removing bindings you don't want, see the article ClassWizard: Binding Recordset Fields to Table Columns.
When you finish, click OK to close ClassWizard. ClassWizard writes your class files in the specified directory and adds them to your project.
Specifying Information for a New Class
Selecting a Data Source in ClassWizard
Selecting a Database Table in ClassWizard
See Also ClassWizard: Database Support, ClassWizard: Binding Recordset Fields to Table Columns