CDaoDatabase Class

Represents a connection to a database through which you can operate on the data.

class CDaoDatabase : public CObject




Constructs a CDaoDatabase object. Call Open to connect the object to a database.




Returns nonzero if the database supports transactions.


Returns nonzero if the CDaoDatabase object is updatable (not read-only).


Closes the database connection.


Creates the underlying DAO database object and initializes the CDaoDatabase object.


Defines a new relation among the tables in the database.


Deletes a querydef object saved in the database's QueryDefs collection.


Deletes an existing relation between tables in the database.


Deletes the definition of a table in the database. This deletes the actual table and all of its data.


Executes an action query. Calling Execute for a query that returns results throws an exception.


Returns the connection string used to connect the CDaoDatabase object to a database. Used for ODBC.


Returns the name of the database currently in use.


Returns the number of queries defined for the database.


Returns information about a specified query defined in the database.


Returns the number of seconds after which database query operations will time out. Affects all subsequent open, add new, update, and edit operations and other operations on ODBC data sources (only) such as Execute calls.


Returns the number of records affected by the last update, edit, or add operation or by a call to Execute.


Returns the number of relations defined between tables in the database.


Returns information about a specified relation defined between tables in the database.


Returns the number of tables defined in the database.


Returns information about a specified table in the database.


Returns the version of the database engine associated with the database.


Returns nonzero if the CDaoDatabase object is currently connected to a database.


Establishes a connection to a database.


Sets the number of seconds after which database query operations (on ODBC data sources only) will time out. Affects all subsequent open, add new, update, and delete operations.




A pointer to the underlying DAO database object.


A pointer to the CDaoWorkspace object that contains the database and defines its transaction space.

For information about the database formats supported, see the GetName member function. You can have one or more CDaoDatabase objects active at a time in a given "workspace," represented by a CDaoWorkspace object. The workspace maintains a collection of open database objects, called the Databases collection.

Note Note

The MFC DAO database classes are distinct from the MFC database classes based on ODBC. All DAO database class names have the "CDao" prefix. Class CDaoDatabase supplies an interface similar to that of the ODBC class CDatabase. The main difference is that CDatabase accesses the DBMS through Open Database Connectivity (ODBC) and an ODBC driver for that DBMS. CDaoDatabase accesses data through a Data Access Object (DAO) based on the Microsoft Jet database engine. 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.


You can create database objects implicitly, when you create recordset objects. But you can also create database objects explicitly. To use an existing database explicitly with CDaoDatabase, do either of the following:

  • Construct a CDaoDatabase object, passing a pointer to an open CDaoWorkspace object.

  • Or construct a CDaoDatabase object without specifying the workspace (MFC creates a temporary workspace object).

To create a new Microsoft Jet (.MDB) database, construct a CDaoDatabase object and call its Create member function. Do not call Open after Create.

To open an existing database, construct a CDaoDatabase object and call its Open member function.

Any of these techniques appends the DAO database object to the workspace's Databases collection and opens a connection to the data. When you then construct CDaoRecordset, CDaoTableDef, or CDaoQueryDef objects for operating on the connected database, pass the constructors for these objects a pointer to your CDaoDatabase object. When you finish using the connection, call the Close member function and destroy the CDaoDatabase object. Close closes any recordsets you have not closed previously.


Database transaction processing is supplied at the workspace level — see the BeginTrans, CommitTrans, and Rollback member functions of class CDaoWorkspace.

ODBC Connections

The recommended way to work with ODBC data sources is to attach external tables to a Microsoft Jet (.MDB) database.


Each database maintains its own collections of tabledef, querydef, recordset, and relation objects. Class CDaoDatabase supplies member functions for manipulating these objects.

Note Note

The objects are stored in DAO, not in the MFC database object. MFC supplies classes for tabledef, querydef, and recordset objects but not for relation objects.

Header: afxdao.h