DAO Recordset: Architecture

OverviewHow Do IFAQSampleODBC Driver List

This article applies to the MFC DAO classes. For ODBC recordsets, see the article Recordset: Architecture (ODBC).

This article describes the data members that make up the architecture of a recordset object:

  • Field data members

  • Parameter data members

  • m_nFields and m_nParams data members

A Sample DAO Recordset Class

When you use ClassWizard to declare a recordset class derived from , the resulting class has the general structure shown in the following simple class:

class CCourseSet : public CDaoRecordset
{
public:
  CCourseSet(CDaoDatabase* pDatabase = NULL);
  DECLARE_DYNAMIC(CCourseSet)

// Field/Param Data
  //{{AFX_FIELD(CCourseSet, CDaoRecordset)
  CString m_CourseID;
  CString m_CourseTitle;
  //}}AFX_FIELD
  CString m_IDParam;

// Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CCourseSet)
  public:
  virtual CString GetDefaultDBName( );
  virtual CString GetDefaultSQL( );
  virtual void DoFieldExchange(CDaoFieldExchange* pFX);
  //}}AFX_VIRTUAL

// Implementation
// ...

};

Near the beginning of the class, ClassWizard writes a set of field data members inside the //{{AFX_FIELD delimiters. When you create the class with ClassWizard, you must specify one or more field data members. If the class is parameterized, as the sample class is (with the data member m_strIDParam), you must manually add parameter data members. ClassWizard doesn’t support adding parameters to a class.

DAO Field Data Members

The most important members of your recordset class are the field data members. For each column you select from the data source, the class contains a data member of the appropriate data type for that column. For example, the sample class shown at the beginning of this article has two field data members, both of type , called m_CourseID and m_CourseTitle.

When the recordset selects a set of records, the framework automatically binds the columns of the current record (after the call, the first record is current) to the field data members of the object. That is, the framework uses the appropriate field data member as a buffer in which to store the contents of a record column (field).

As the user scrolls to a new record, the framework uses the field data members to represent the current record. The framework refreshes the field data members, replacing the previous record’s values. The field data members are also used for updating the current record and for adding new records. As part of the process of updating a record, you specify the update values by assigning values directly to the appropriate field data member(s).

DAO Parameter Data Members

If the class is parameterized, it has one or more parameter data members. A parameterized class lets you base a recordset query on information obtained or calculated at run time. (For an alternative approach to parameterizing a recordset by using a querydef, see the article DAO Queries: Filtering and Parameterizing Queries.)

****Note   ****You must manually place these data members outside the //{{AFX_FIELD comment brackets.

Typically, the parameter helps narrow the selection, as in the following example. Based on the sample class at the beginning of this article, the recordset object might execute the following SQL statement:

SELECT CourseID, CourseTitle FROM Course WHERE CourseID = [Course Ident]

The [Course Ident] is a named parameter whose value you supply at run time. When you construct the recordset and set its m_strIDParam data member to “MATH101”, the effective SQL statement for the recordset becomes:

SELECT CourseID, CourseTitle FROM Course WHERE CourseID = MATH101

****Note   ****This is the effective SQL, but the actual SQL works more efficiently. In particular, it does not do a simple text replacement.

The square brackets are only required if the column or parameter name contains spaces.

By defining parameter data members, you tell the framework about parameters in the SQL string. The framework binds the parameter, which lets DAO know where to get values to substitute for the parameter name. In the example, the resulting recordset contains only the record from the Course table with a CourseID column whose value is “MATH101”. All specified columns of this record are selected. You can specify as many parameters (and named placeholders for them) as you need.

****Note   ****MFC does nothing itself with the parameters — in particular, it doesn’t perform a text substitution. Instead, MFC gives the parameter values to DAO, which uses them.

****Important   ****The name of a parameter is important. For details about this and more information about parameters, see the article DAO Queries: Filtering and Parameterizing Queries.

Using m_nFields and m_nParams with DAO

When ClassWizard writes a constructor for your class, it also initializes the m_nFields data member, which specifies the number of field data members in the class. If you add any parameters to your class, you must also add an initialization for the data member, which specifies the number of parameter data members. The framework uses these values to work with the data members.

For more information and examples, see the articles DAO Record Field Exchange (DFX) and DAO Queries: Filtering and Parameterizing Queries. For related information, see the following topics in DAO Help:

  • Creating Parameter Queries with DAO

  • PARAMETERS Declaration (SQL)

See Also   DAO: Where Is..., DAO Recordset, DAO Queries, DAO Querydef