class CDaoQueryDef : public CObject
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.
Use querydef objects either to work with an existing saved query or to create a new saved query or temporary query:
- In all cases, first construct a CDaoQueryDef object, supplying a pointer to the CDaoDatabase object to which the query belongs.
- 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.
You can use a querydef object for any of the following purposes:
- To create a CDaoRecordset object
- To call the object's Execute member function to directly execute an action query or a SQL pass-through query
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.
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 connection string that points to the external data source. Queries with valid connection 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.
For more information about querydefs, see the article DAO Querydef. For related information, see the topics "QueryDef Object", "QueryDefs Collection", and "CdbDatabase Object" in the DAO SDK.