Using Microsoft Foundation Classes

The CRecordset C++ class provided with Microsoft Foundation Classes (MFC) uses the SQLPrepare function to prepare queries before execution. You can use the CRecordset::Requery function to re-execute the query for the CRecordset class without requiring the ODBC driver to re-parse the SQL statement, because the statement has already been prepared.

By default, MFC library classes load the Cursor Library. The Cursor Library permits updatable snapshots. To get updatable recordsets, but not use the Cursor Library, use the dynaset Crecordset class. To specify it, pass CRecordset::dynaset for the first argument of CRecordset::Open. With the 32-bit MFC library database classes, it’s not enough to pass CRecordset::dynaset to CRecordset::Open. You must pass FALSE for the last argument of CDatabase::Open. This prevents the Cursor Library from loading. The code looks like this:

CDatabase db;
db.Open("DataSourceName",FALSE,FALSE,"ODBC;",FALSE);
CYourRecordset rs(&db);
rs.Open(CRecordset::dynaset);

To use dynasets, the DYNSET.EXE code, or 32-bit classes use the SQLSetPos functionality of the Microsoft Access Desktop Database Driver when you are performing updates, deletions, or insertions. As mentioned previously, this greatly increases the speed of an application.

The Microsoft Access Desktop Database Driver does not support MFC transactions. The MFC classes require ODBC drivers to support Recordset object cursor preservation across rollbacks and commits of transactions.

The Microsoft Access Desktop Database Driver does not guarantee this cursor preservation; however, you can use transactions if you requery after any transaction so that the cursor is restored to the first record in the Recordset object. Force the m_bTransactions member variable of the Database object to True before using the BeginTrans method. The code could look like this:

class CTransactDatabase: public CDatabase
{
    public:
    void SetTransactions(){ m_bTransactions=TRUE;}
};
.
.
.
CTransactDatabase db;
db.Open("SomeDataSourceName",FALSE,FALSE,"ODBC;",FALSE);
db.SetTransactions();
db.BeginTrans();
CRecordSet rs(&db);
rs.Open(CRecordset::dynaset);
.
// Manipulate data.
.
db.CommitTrans(); // or db.Rollback()
::SQLFreeStmt(rs.m_hstmt, SQL_CLOSE);
db.BeginTrans();
rs.Requery();
.
// Manipulate data.
.
db.CommitTrans() // or db.Rollback()
rs.Close();
db.Close();
.

See Also For more information, see the documentation for SQLGetInfo and the SQL_CURSOR_COMMIT_BEHAVIOR and SQL_CURSOR_ROLLBACK_
BEHAVIOR parameters in the Microsoft ODBC 3.0 Software Development Kit and Programmer’s Reference.