CDaoQueryDef

A CDaoQueryDef object represents a query definition, or “querydef,” usually one saved in a database. A querydef is a data access object that contains the SQL statement that describes a query, and its properties, such as “Date Created” and “ODBC Timeout.” You can also create temporary querydef objects without saving them, but it is convenient — and much more efficient — to save commonly reused queries in a database. A CDaoDatabase object maintains a collection, called the QueryDefs collection, that contains its saved querydefs.

Note   The DAO database classes are distinct from the MFC database classes based on Open Database Connectivity (ODBC). All DAO database class names have the “CDao” prefix. You can still access ODBC data sources with the DAO classes. In general, the MFC classes based on DAO are more capable than the MFC classes based on ODBC; the DAO-based classes can access data, including through ODBC drivers, via their own database engine. The DAO-based classes also support Data Definition Language (DDL) operations, such as adding tables via the classes, without having to call DAO directly.

Usage

Use querydef objects either to work with an existing saved query or to create a new saved query or temporary query:

  1. In all cases, first construct a CDaoQueryDef object, supplying a pointer to the CDaoDatabase object to which the query belongs.

  2. Then do the following, depending on what you want:
    • To use an existing saved query, call the querydef object’s Open member function, supplying the name of the saved query.

    • To create a new saved query, call the querydef object’s Create member function, supplying the name of the query. Then call Append to save the query by appending it to the database’s QueryDefs collection. Create puts the querydef into an open state, so after calling Create you do not call Open.

    • To create a temporary querydef, call Create. Pass an empty string for the query name. Do not call Append.

When you finish using a querydef object, call its Close member function; then destroy the querydef object.

Tip   The easiest way to create saved queries is to create them and store them in your database using Microsoft Access. Then you can open and use them in your MFC code.

Purposes

You can use a querydef object for any of the following purposes:

You can use a querydef object for any type of query, including select, action, crosstab, delete, update, append, make-table, data definition, SQL pass-through, union, and bulk queries. The query’s type is determined by the content of the SQL statement that you supply. For information about query types, see the Execute and GetType member functions. Recordsets are commonly used for row-returning queries, usually those using the SELECT ... FROM keywords. Execute is most commonly used for bulk operations. For more information, see Execute and CDaoRecordset.

Querydefs and Recordsets

To use a querydef object to create a CDaoRecordset object, you typically create or open a querydef as described above. Then construct a recordset object, passing a pointer to your querydef object when you call CDaoRecordset::Open. The querydef you pass must be in an open state. For more information, see class CDaoRecordset.

You cannot use a querydef to create a recordset (the most common use for a querydef) unless it is in an open state. Put the querydef into an open state by calling either Open or Create.

External Databases

Querydef objects are the preferred way to use the native SQL dialect of an external database engine. For example, you can create a Transact SQL query (as used on Microsoft SQL Server) and store it in a querydef object. When you need to use a SQL query not based on the Microsoft Jet database engine, you must provide a connect string that points to the external data source. Queries with valid connect strings bypass the database engine and pass the query directly to the external database server for processing.

Tip   The preferred way to work with ODBC tables is to attach them to a Microsoft Jet (.MDB) database. For more information, see the article DAO External: Working with External Data Sources in Visual C++ Programmer's Guide.

For more information about querydefs, see the article DAO Querydef in Visual C++ Programmer's Guide. For related information, see the topics "QueryDef Object", "QueryDefs Collection", and "Accessing External Databases with DAO" in DAO Help.

#include <afxdao.h>

Class MembersBase ClassHierarchy Chart

Samples   MFC Sample DAOVIEWMFC Sample DAOTABLE

See Also   CDaoRecordset, CDaoDatabase, CDaoTableDef, CDaoException