SAMPLE: Using Dynasets with the 16-Bit MFC Database Classes

ID: Q124915


The information in this article applies to:


SUMMARY

With some additional code, dynasets may be used with the MFC 2.51 or 2.52. This sample contains the following components:


   DYNA.H          - Single header file used by next three files
   DYNACORE.CPP    - Defines CDynaRecordset and CDynaDatabase
   DYNARFX.CPP     - Defines new RFX functions
   DYNAVIEW.CPP    - Defines CDynaRecordView
   ENROLL          - Modified ENROLL Step 3 sample that
                     demonstrates how the code can be used. 
To successfully use this code, you must have:

NOTE: The file must be decompressed by typing "DYNSET.EXE -d".

The following file is available for download from the Microsoft Software Library:
Dynset.exe
For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services


MORE INFORMATION

What Is a Dynaset?

The Visual C++ MFC Encyclopedia in the books online states the following:
A dynaset is a recordset with dynamic properties. During its lifetime, a recordset object in dynaset mode (usually called simply a "dynaset") stays synchronized with the data source in the following way. In a multiuser environment, other users may edit or delete records that are in your dynaset or add records to the table your dynaset represents. Records your application updates or deletes from the recordset are reflected in your dynaset. Records that other users add to the table will not be reflected in your dynaset until you rebuild the dynaset by calling its Requery member function. Records that other users delete appear as "holes" in your recordset, marked as "deleted." Other users' editing changes to existing records are reflected in your dynaset as soon as you scroll to the affected record.

Similarly, edits you make to records in a dynaset are reflected in dynasets in use by other users. Records you add are not reflected in other users' dynasets until they requery their dynasets. Records you delete are marked as "deleted" in others' recordsets. If you have multiple connections to the same database (multiple CDatabase objects), recordsets associated with those connections have the same status as the recordsets of other users. Dynasets are most valuable when data must be dynamic, as for an airline reservation system.

What Are the Advantages Of Using Dynasets?

As mentioned above, dynaset recordsets read directly from the data source for each record fetched in the recordset. Therefore the record can change values between subsequent fetches. There are two additional big advantages which dynasets provide: speed and decreased memory usage. By not using snapshots and using dynasets instead, you don't load the cursor library (ODBCCURS.DLL). The cursor library reads each record for a recordset and stores it into memory as you traverse through the records. Thus, it saves the data for that moment in time. This can slow application performance. The cursor library creates temporary files, which are used for storing the temporary records. This uses more memory and disk space which you wouldn't use if you use dynasets. Lastly, the cursor library traps ODBC API calls before they are sent to the driver and this can slow an application.

What Are the Requirements for Using Dynasets?

Not all ODBC drivers support MFC dynasets. For example, the ODBC drivers included with Visual C++ version 1.5, 1.51, and 1.52 do not support dynasets. If you use the ODBC drivers provided with the ODBC 2.0 Desktop Driver Pack and the code provided with this sample, you can utilize dynasets for the desktop drivers (Microsoft Access 2.0, FoxPro, and so forth).

When using this code, it is important that your ODBC driver support the following:

Look at the CDynaRecordset::OnSetOptions() function in DYNACORE.CPP to see the SQLGetInfo() calls that are used to determine if a driver supports MFC dynasets. Specifically, SQL_SO_KEYSET_DRIVEN and SQL_POS_OPERATIONS functionality is checked. If SQL_POS_OPERATIONS isn't supported by the driver, the dynaset can not be modified and therefore will be read-only.

Look at the help file or other documentation for the ODBC driver you're using to discover whether it supports the items mentioned above.

NOTE: The ODBC drivers that come with Visual C++ 1.5 and 1.51 don't support SQLSetPos(). You must be using the ODBC drivers provided with the ODBC 2.0 Driver pack, or something newer.

How to Use the Code

A modified version of the ENROLL Step 3 tutorial sample is provided. It demonstrates how to use the dynaset code. To use the code do the following:

Why Are the ODBC 2.1 Components Necessary?

The ODBC 2.1 Driver Manager is required to avoid a bug that occurs with the previous versions of the driver manager. If you use the driver manager (ODBC.DLL) included in the ODBC Driver Pack, you will receive an "Invalid Cursor State" error when using dynasets and trying to add a record to a table following an query that produced an empty recordset. In other words, if you open a CRecordset object and the query returns an empty recordset, a subsequent AddNew()/Update() call will cause the error. The ODBC 2.1 driver fixes this problem. The ODBC 2.1 components shipped with Visual C++ 1.52.


REFERENCES

For more information about the ODBC functions used, see the ODBC 2.0 Programmer's Reference. This is included in the Visual C++ version 2.0 online books.

Additional query words: dynamic dyna set record set helpfile


Keywords          : kbfile kbprg kbsample kb16bitonly kbDatabase kbMFC kbODBC kbVC 
Version           : 1.51 1.52
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: July 30, 1999