共用方式為


CDaoDatabase Class

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

class CDaoDatabase : public CObject

Members

Public Constructors

Name

Description

CDaoDatabase::CDaoDatabase

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

Public Methods

Name

Description

CDaoDatabase::CanTransact

Returns nonzero if the database supports transactions.

CDaoDatabase::CanUpdate

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

CDaoDatabase::Close

Closes the database connection.

CDaoDatabase::Create

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

CDaoDatabase::CreateRelation

Defines a new relation among the tables in the database.

CDaoDatabase::DeleteQueryDef

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

CDaoDatabase::DeleteRelation

Deletes an existing relation between tables in the database.

CDaoDatabase::DeleteTableDef

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

CDaoDatabase::Execute

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

CDaoDatabase::GetConnect

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

CDaoDatabase::GetName

Returns the name of the database currently in use.

CDaoDatabase::GetQueryDefCount

Returns the number of queries defined for the database.

CDaoDatabase::GetQueryDefInfo

Returns information about a specified query defined in the database.

CDaoDatabase::GetQueryTimeout

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.

CDaoDatabase::GetRecordsAffected

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

CDaoDatabase::GetRelationCount

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

CDaoDatabase::GetRelationInfo

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

CDaoDatabase::GetTableDefCount

Returns the number of tables defined in the database.

CDaoDatabase::GetTableDefInfo

Returns information about a specified table in the database.

CDaoDatabase::GetVersion

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

CDaoDatabase::IsOpen

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

CDaoDatabase::Open

Establishes a connection to a database.

CDaoDatabase::SetQueryTimeout

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.

Public Data Members

Name

Description

CDaoDatabase::m_pDAODatabase

A pointer to the underlying DAO database object.

CDaoDatabase::m_pWorkspace

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

Remarks

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

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.

Usage

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.

Transactions

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.

Collections

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

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.

Inheritance Hierarchy

CObject

CDaoDatabase

Requirements

Header: afxdao.h

See Also

Reference

CObject Class

Hierarchy Chart

CDaoWorkspace Class

CDaoRecordset Class

CDaoTableDef Class

CDaoQueryDef Class

CDatabase Class

CDaoException Class