Partager via


CDatabase::BeginTrans

Call this member function to begin a transaction with the connected data source.

BOOL BeginTrans( );

Return Value

Nonzero if the call was successful and changes are committed only manually; otherwise 0.

Remarks

A transaction consists of one or more calls to the AddNew, Edit, Delete, and Update member functions of a CRecordset object. Before beginning a transaction, the CDatabase object must already have been connected to the data source by calling its OpenEx or Open member function. To end the transaction, call CommitTrans to accept all changes to the data source (and carry them out) or call Rollback to abort the entire transaction. Call BeginTrans after you open any recordsets involved in the transaction and as close to the actual update operations as possible.

Warning

Depending on your ODBC driver, opening a recordset before calling BeginTrans may cause problems when calling Rollback. You should check the specific driver you are using. For example, when using the Microsoft Access driver included in the Microsoft ODBC Desktop Driver Pack 3.0, you must account for the Jet database engine's requirement that you should not begin a transaction on any database that has an open cursor. In the MFC database classes, an open cursor means an open CRecordset object. For more information, see Technical Note 68.

BeginTrans may also lock data records on the server, depending on the requested concurrency and the capabilities of the data source. For information about locking data, see the article Recordset: Locking Records (ODBC).

User-defined transactions are explained in the article Transaction (ODBC).

BeginTrans establishes the state to which the sequence of transactions can be rolled back (reversed). To establish a new state for rollbacks, commit any current transaction, then call BeginTrans again.

Warning

Calling BeginTrans again without calling CommitTrans or Rollback is an error.

Call the CanTransact member function to determine whether your driver supports transactions for a given database. You should also call GetCursorCommitBehavior and GetCursorRollbackBehavior to determine the support for cursor preservation.

For more information about transactions, see the article Transaction (ODBC).

Example

See the article Transaction: Performing a Transaction in a Recordset (ODBC).

Requirements

Header: afxdb.h

See Also

Reference

CDatabase Class

Hierarchy Chart

CDatabase::CommitTrans

CDatabase::Rollback

CRecordset::CanTransact