CDaoDatabase Class

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

class CDaoDatabase : public CObject


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.


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.


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

See Also


CObject Class

Hierarchy Chart

CDaoWorkspace Class

CDaoRecordset Class

CDaoTableDef Class

CDaoQueryDef Class

CDatabase Class

CDaoException Class

Other Resources

CDaoDatabase Members