DAO and MFC

类别:编程语言 点击:0 评论:0 推荐:
DAO and MFC

This article describes MFC's implementation of Microsoft Data Access Objects (DAO). Topics covered include: How MFC Encapsulates DAO Mapping of DAO objects to MFC classes Key differences between MFC and DAO Further reading about the MFC DAO classes 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). How MFC Encapsulates DAO

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 fields of a recordset object The indexes or fields of a table The parameters of a querydef The relations defined between tables in a database Mapping of DAO Objects to MFC Classes

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.

Note   MFC now supports DAO 3.5. MFC DAO classes work either with DAO 3.0 or DAO 3.5, but have not been designed to take advantage of any new DAO 3.5 features, including ODBCDirect.

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. DAO Objects Not Exposed in MFC

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.

Accessing the Unexposed DAO Objects

The unexposed objects listed above can be accessed in two ways: Outside the MFC classes by using the non-MFC C++ classes provided in the DAO SDK. Inside the MFC classes by calling DAO directly through a DAO interface pointer supplied by one of the MFC classes. For information, see Technical Note 54. Key Differences Between MFC and DAO

MFC's version of data access objects differs from the underlying structure of DAO in some ways.

How MFC Accesses the Database Engine

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.

MFC Flattening of the DAO Object Hierarchy

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: Putting access to the database engine in class CDaoWorkspace rather than in a database engine class. Encapsulating DAO field, index, parameter, and relation objects inside the classes that represent their owning objects. For example, access to fields is encapsulated in classes CDaoTableDef and CDaoRecordset. For information, see the table How MFC Manages DAO Objects Not Mapped to Classes. MFC and DAO Security

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.

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.

Further Reading About the MFC DAO Classes

To learn more about using the MFC DAO classes, see the following articles (in the order listed here): DAO: Writing a Database Application DAO: Database Tasks DAO: Creating, Opening, and Closing DAO Objects DAO Workspace DAO Database DAO Database: Using Workspaces and Databases DAO Recordset DAO Record Field Exchange (DFX) DAO Querydef DAO Tabledef DAO Workspace: Managing Transactions DAO Collections DAO External: Working with External Data Sources (primarily ODBC) DAO Workspace: The Database Engine DAO: Using DAO in DLLs Exceptions: Database Exceptions Record Views 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.

本文地址:http://com.8s8s.com/it/it24650.htm