次の方法で共有


DAO Recordset: Creating Recordsets

OverviewHow Do IFAQSampleODBC Driver List

This article explains the process of creating recordset objects based either on class itself or on a class derived from CDaoRecordset.

You can use recordset objects in two ways:

In either case, you can base your recordset objects on a query defined by a object or a object, or you can specify the recordset objects’ SQL strings either at design time, when you create the class with a wizard, or at run time, when you pass a string containing an SQL statement to the member function.

The following table describes the characteristics of a recordset, depending on how you create it:

If you base your recordset on... It has these characteristics...
A querydef The recordset inherits the querydef’s SQL string.
A tabledef The recordset (a table-type recordset) is based on the table. Creating a recordset from a tabledef is similar to creating one from a querydef. See the following procedure for creating from a querydef.
The SQL string specified at design time, in the wizard The recordset uses the SQL string retrieved by calling its member function. The wizard codes the string as the value returned by this function.
An SQL string specified at run time, in the Open call The SQL string passed to Open overrides the SQL defined at design time.

The following procedure shows how to use a querydef as the basis for a recordset. Using a tabledef to create a recordset is similar.

To create a recordset from a querydef

  1. Create the querydef, as described in Creating a Query with a Querydef, or use an existing querydef object.

  2. Construct a object.

  3. Call the recordset object's member function, passing a pointer to the querydef object.

Opening the recordset runs the query, using the SQL statement defined by the querydef.

****Note   ****You can only create a dynaset-type or snapshot-type recordset from a querydef.

The following code shows the process of creating a recordset from a querydef:

// pdb is a pointer to an open CDaoDatabase object
try
{
    // Construct the querydef
    CDaoQueryDef qd( pdb );

    // Set up the SQL string and create the querydef
    CString strSQL =
      _T(“SELECT [Company Name] FROM Publishers WHERE State = ‘NY’”);
    qd.Create( _T(“My Querydef”), strSQL );

    // Construct a CDaoRecordset-derived object
    // and open it based on the querydef
    CPublisherSet rsPub( pdb );
    rsPub.Open( &qd );
}
catch( CDaoException* e )
// ...

To create a recordset without a querydef

  1. Construct a object.

  2. Call the recordset object's member function. Specify an SQL string, or rely on the one that the wizard creates.

For an example in code, see the article DAO Recordset: Creating Recordsets.

The SQL statement for the recordset is one of the following:

  • The default SQL string that you established when you created the recordset class using AppWizard or ClassWizard. If you pass NULL for the lpszSQL parameter to Open, the recordset uses the default SQL string. The recordset obtains the default SQL string by calling its own member function. The default SQL string is coded as the value returned by that function rather than being stored in a data member.

  • An SQL string that you pass in your call to Open. This string overrides the default SQL string defined in the recordset class.

In either case, your SQL string can consist of any of the following:

  • A SELECT statement of the basic form:

    SELECT column-list FROM table-list
    
  • One or more tabledef and/or querydef names, separated by commas, which base the query on tables and/or queries. This list is usually based on choices you made with one of the wizards. The query selects all columns (fields) in the tables/queries, unless you modify it.

For details, see . For related information, see the topic "Recordset Object" in DAO Help.

For information about SQL, see the topic "SQL Property" in DAO Help.

For information about the SQL syntax used by DAO, see the topic "Comparison of Microsoft Jet Database Engine SQL and ANSI SQL" in DAO Help.

See Also   DAO: Where Is..., DAO Recordset, DAO Querydef, DAO Record Field Exchange (DFX), DAO Queries: SQL for DAO, DAO Queries