Share via


DAO Database: Using Workspaces and Databases

OverviewHow Do IFAQSampleODBC Driver List

This article explains how to use and objects. Topics covered include:

  • A typical workspace scenario

  • Transactions in the typical scenario

  • Beyond the typical scenario

A Typical Workspace Scenario

In the majority of data access applications, you work less at the workspace level than at the database or even recordset level. It might seem the normal thing to construct an explicit object, then from it construct a object and from that construct , , and objects. But the more typical approach is one of the following:

  • Construct a CDaoDatabase object, perhaps stored in your -derived class. Then from it construct the necessary recordsets and other objects. You're likely to do this if you want to maintain a connection to a single database for the life of your application, or at least the life of your document. For related information, see the articles MFC: Using Database Classes with Documents and Views, MFC: Using Database Classes Without Documents and Views, and DAO: Writing a Database Application.

  • Construct recordsets as needed, relying on MFC to create the necessary and objects behind the scenes. You're likely to do this if you prefer to construct recordsets within the scope of a function, for example to run a query based on a menu command.

    ****Note   ****This is inefficient if you are continually opening and closing the same database. In that case, create an explicit CDaoDatabase object and use it for the life of your application.

Transactions in the Typical Scenario

The primary action taken on a workspace object that might be called typical is to use the object for transactions against one or more databases. The transaction commands in MFC are members of class .

To access transaction commands in the most typical case, you can use the implicit workspace that MFC creates behind and objects (one implicit workspace for multiple objects). To issue transaction commands, such as , , or , you can choose to call those member functions of CDaoWorkspace through the pointer stored in your CDaoRecordset or CDaoDatabase object. For details about accessing such pointers, see the article DAO: Accessing Implicit MFC DAO Objects.

For example, from a recordset object, you might call:

// prs is a pointer to an already opened
// CDaoRecordset object
prs->m_pDatabase->m_pWorkspace->BeginTrans( );
...

Beyond the Typical Scenario

The typical scenario is not enough in some fairly rare cases. For a discussion of when you might need an explicit object, see the article DAO Workspace.

See Also   DAO: Where Is..., DAO Workspace: Managing Transactions, DAO Workspace, DAO Database, DAO: Creating, Opening, and Closing DAO Objects