When you want to access a table data, the Visual C++ 6.0 IDE lets you easily add to the project a new MFC class, derived from either CRecordset or CDaoRecordset. After having specified the data source and the concerned table, a class is automatically generated, which needs to specifically define a certain number of variables to be used for the communication with the database and a possible visualization window. Such communication is established through MFC specialized functions, which compel the new class to know the accessed fields type.
The classes CBufferRecSet and CBufferDaoRecSet, respectively derived from CRecordset and CDaoRecordset, have been planned to free from the quoted needs: these classes manage a dynamic buffer in order to automatically associate a variable to each recordset field, and besides contain methods to perform the data exchange between the Database and the buffer (DoFieldExchange) and between the buffer and a visualization window (DDX_FieldVariant), taking charge of calling the right MFC functions, according to the concerned field type. Furthermore, they provide methods to directly access the buffer variables, either in reading or in writing, and let you define a filter on the recordset during the Open call, passing as parameters the filtered field name and a pointer to a variable containing the referenced value (currently it's not allowed to filter more than one field). Their implementation keeps the compatibility with the MFC classes CRecordView and CDaoRecordView respectively.
Let's see in the following listings an example of the simplification obtained using the classes CBufferRecSet and CBufferDaoRecSet instead of the original ones CRecordset and CDaoRecordset. On the left you can find the source code as the AppWizard has generated it, while on the right you can find the corresponding source code as modified in order to use the new classes. This is the meaning of the colours:
modified source code | deleted source code | added source code |
After having created a new MFC AppWizard (exe) project, with the options Single document and Database view with file support (choosing a DAO Data source accessing the table Orders in the Microsoft Access Database sample - Northwind.mdb), I have added to the generated dialog box as many edit boxes as fields in the table, and then, with the ClassWizard, I have connected the edit boxes with the variables associated to the fields.
class CDaoDBSet : public CDaoRecordset
|
class CDaoDBSet : public CBufferDaoRecSet
|
CDaoDBSet::CDaoDBSet(CDaoDatabase* pdb)
|
CDaoDBSet::CDaoDBSet(CDaoDatabase* pdb)
|
void CDaoDBSet::DoFieldExchange(CDaoFieldExchange* pFX)
|
|
void CDaoDBView::DoDataExchange(CDataExchange* pDX)
|
void CDaoDBView::DoDataExchange(CDataExchange* pDX)
|
After having created a new MFC AppWizard (exe) project, with the options Single document and Database view with file support (choosing an ODBC Data source accessing the table Orders in the Microsoft Access Database sample - Northwind.mdb), I have added to the generated dialog box as many edit boxes as fields in the table, and then, with the ClassWizard, I have connected the edit boxes with the variables associated to the fields.
class CODBCDBSet : public CRecordset
|
class CODBCDBSet : public CBufferRecSet
|
CODBCDBSet::CODBCDBSet(CDatabase* pdb)
|
CODBCDBSet::CODBCDBSet(CDatabase* pdb)
|
void CODBCDBSet::DoFieldExchange(CFieldExchange* pFX)
|
|
void CODBCDBView::DoDataExchange(CDataExchange* pDX)
|
void CODBCDBView::DoDataExchange(CDataExchange* pDX)
|