Home | Overview | How Do I | FAQ | Sample | Tutorial | ODBC Driver List
This article describes MFC’s implementation of Microsoft Data Access Objects (DAO). Topics covered include:
Note Whether you use the MFC DAO classes or the MFC ODBC classes depends on your situation and your needs. For a discussion of the differences between the two and guidance on choosing one, see the article Database Topics (DAO).
Caution DAO is not supported on Win32s. Further, DAO 3.x is not thread-safe. You should use DAO only in the primary thread of an application.
The MFC DAO classes treat DAO much as the MFC classes for programming Windows treat the Windows API: MFC encapsulates, or "wraps," DAO functionality in a number of classes that correspond closely to DAO objects. Class CDaoWorkspace encapsulates the DAO workspace object, class CDaoRecordset encapsulates the DAO recordset object, class CDaoDatabase encapsulates the DAO database object, and so on.
MFC's encapsulation of DAO is thorough, but it is not completely one-for-one. Most major DAO objects do correspond to an MFC class, and the classes supply generally thorough access to the underlying DAO object's properties and methods. But some DAO objects, including fields, indexes, parameters, and relations, do not. Instead, the appropriate MFC class provides an interface, via member functions, through which you can access, for example:
The following tables show how DAO objects correspond to MFC objects. MFC Classes and Corresponding DAO Objects shows the MFC classes and the DAO objects they encapsulate. How MFC Manages DAO Objects Not Mapped to Classes shows how MFC deals with DAO objects that do not map directly to an MFC class.
MFC Classes and Corresponding DAO Objects
Class | DAO object | Remarks |
CDaoWorkspace | Workspace | Manages a transaction space and provides access to the database engine. |
CDaoDatabase | Database | Represents a connection to a database. |
CDaoTableDef | Tabledef | Used to examine and manipulate the structure of a table. |
CDaoQueryDef | Querydef | Used to store queries in a database. You can create recordsets from a querydef or use it to execute action or SQL pass-through queries. |
CDaoRecordset | Recordset | Used to manage a result set, a set of records based on a table or selected by a query. |
CDaoException | Error | MFC responds to all DAO errors by throwing exceptions of this type. |
CDaoFieldExchange | None | Manages exchange of data between a record in the database and the field data members of a recordset. |
How MFC Manages DAO Objects Not Mapped to Classes
DAO object | How MFC manages it |
Field | Objects of classes CDaoTableDef and CDaoRecordset encapsulate fields and supply member functions for adding them, deleting them, and examining them. |
Index | Objects of classes CDaoTableDef and CDaoRecordset encapsulate indexes and supply member functions for managing them. Tabledefs can add, delete, and examine indexes. Tabledefs and recordsets can set or get the currently active index. |
Parameter | Objects of class CDaoQueryDef encapsulate parameters and supply member functions for adding them, deleting them, examining them, and getting and setting their values. |
Relation | Objects of class CDaoDatabase encapsulate relations and supply member functions for adding them, deleting them, and examining them. |
MFC and DAO do not supply abstractions for some objects used within Microsoft Access: Application, Container, Control, Debug, Document, Form, Module, Report, Screen, and Section. If you create a Microsoft Access database and manipulate it from an MFC application, you can’t access those objects through code.
MFC doesn’t supply classes or interfaces to the DAO group and user objects ? to work with DAO security, you must write your own code.
MFC also doesn’t encapsulate DAO property objects, except that the MFC DAO classes do give you access to the properties of all exposed objects.
MFC does give you access to DAO’s DBEngine object, through class CDaoWorkspace.
The unexposed objects listed above can be accessed in two ways:
MFC's version of data access objects differs from the underlying structure of DAO in some ways.
DAO has a DBEngine object that represents the Microsoft Jet database engine. The DBEngine object provides properties and methods you can use to configure the database engine.
In MFC, there is no DBEngine object. Access to important properties of the database engine is supplied via class CDaoWorkspace. To set or get these properties, call any of the static member functions of CDaoWorkspace. For more information, see the articles DAO Workspace: The Database Engine and DAO Workspace: Accessing Properties of the Database Engine.
Because MFC doesn't supply a class for every DAO object, the effect is that the DAO object hierarchy is somewhat "flattened" in MFC. The main examples of this flattening are:
MFC does not encapsulate the DAO user and group objects in any way, which means that MFC doesn't provide DAO's security functionality.
You can still use DAO security from your MFC applications, but you will have to call DAO directly, using the m_pDAOWorkspace data member of class CDaoWorkspace. That member is a pointer to an interface that gives access to a DAO workspace object's methods and properties. For information about calling DAO directly, see Technical Note 54.
Tip The DAO Software Development Kit (SDK) supplies its own set of C++ classes (not compatible with MFC) for working with DAO. You can use these classes, if you wish, by installing the DAO SDK using \DAOSDK\Disk1\Setup.exe on the Visual C++ CD. These classes provide an additional source of examples for using DAO from C++.
MFC does allow password protection via various MFC classes. For example, when you create a CDaoWorkspace object, you can specify a password to protect the database(s) that the workspace contains. To use this functionality, a SYSTEM.MDW file must be available to the database engine on the machine running your application. If no SYSTEM.MDW file is available to the database engine, your application cannot use any of the security features. For information about the SYSTEM.MDW file, see the topic "Permissions Property" in DAO Help.
To learn more about using the MFC DAO classes, see the following articles (in the order listed here):
Tip From any of the MFC help topics in this documentation set, you can get to a topic called DAO: Where Is..., which helps you navigate online to the topics that you need. The topic is always available via the See Also button in the topic window.
See Also DAO: Where Is...