MFC: Using Database Classes with Documents and Views

HomeOverviewHow Do IFAQ   |  ODBC Driver List

You can use the MFC database classes ? DAO or ODBC ? with or without the document/view architecture. This article emphasizes working with documents and views. It explains:

For alternatives, see the article MFC: Using Database Classes Without Documents and Views.

Writing a Form-Based Application

Many data-access applications are based on forms. The user interface is a form containing controls in which the user examines, enters, or edits data. To make your application form-based, use class CRecordView or CDaoRecordView. You can specify CRecordView or CDaoRecordView for your view class when you run AppWizard, or you can use ClassWizard later to create a CRecordView-derived or CDaoRecordview-derived class.

In a form-based application, each record view object stores a pointer to a CRecordset or CDaoRecordset object. The framework’s record field exchange (RFX) mechanism exchanges data between the recordset and the data source. The dialog data exchange (DDX) mechanism exchanges data between the field data members of the recordset object and the controls on the form. CRecordView or CDaoRecordView also provides default command handler functions for navigating from record to record on the form.

To create a form-based application with AppWizard

To add a database form to your application with ClassWizard

For a full discussion of forms, see the article Record Views. For an example of an application with multiple record views on a database, see the MFC tutorial sample ENROLL, Step 4. Step 4 is not covered in the Data Access Objects (DAO) Tutorial, but you can examine the code in the sample.

Using Recordsets in Documents and Views

Many simple form-based applications don’t need “documents.” If your application is more complex, you’ll probably want to use a document as a proxy for the database, storing a CDatabase or CDaoDatabase object that connects to the data source. Form-based applications usually store a pointer to a recordset object in the view. Other kinds of database applications store recordsets and CDatabase or CDaoDatabase objects in the document. Here are some possibilities for using documents in database applications:

Other Factors

Form-based applications often do not have any use for the framework’s document serialization mechanism, so you might want to remove, disable, or replace the New and Open commands on the File menu. See the article Serialization: Serialization vs. Database Input/Output.

You might also want to make use of the many user-interface possibilities that the framework can support. For example, you could use multiple CRecordView or CDaoRecordView objects in a splitter window, open multiple recordsets in different multiple document interface (MDI) child windows, and so on.

You might want to implement printing of whatever is in your view — whether it’s a form implemented with CRecordView or CDaoRecordView or something else. As classes derived from CFormView, CRecordView and CDaoRecordView don’t support printing, but you can override the OnPrint member function to allow printing. For more information, see class CFormView.

You might not want to use documents and views at all. In that case, see the article MFC: Using Database Classes Without Documents and Views.