HOWTO: Performing Transactions with SQL Server 6.xLast reviewed: September 30, 1997Article ID: Q168708 |
The information in this article applies to:
SUMMARYThis article discusses topics related to performing transactions with the Microsoft SQL Server 6.0 and 6.5 using the MFC ODBC classes. Please refer to the following documentation for more information on MFC database transactions prior to this article:
In the Microsoft Knowledge Base: ARTICLE-ID: Q128208 TITLE : How to Perform Transactions with the MFC Database Classes MFC Tech Note #47 - "Relaxing Database Transaction Requirements." MORE INFORMATIONBelow is a list of transaction related values the SQL Server ODBC driver returns via SQLGetInfo().
SQL Server ODBC InfoType Driver Version Return Value SQL_TXN_CAPABLE 2.50 SQL_TC_DML* 2.65 SQL_TC_ALL* SQL_CURSOR_COMMIT_BEHAVIOR 2.50 SQL_CB_CLOSE 2.65 SQL_CB_CLOSE SQL_CURSOR_ROLLBACK_BEHAVIOR 2.50 SQL_CB_CLOSE 2.65 SQL_CB_CLOSE*SQL_TC_DML = Transactions can only contain Data Manipulation Language (DML) statements (SELECT, INSERT, UPDATE, DELETE). Data Definition Language (DDL) statements encountered in a transaction cause an error.*SQL_TC_ALL = Transactions can contain DDL statements and DML statements in any order. SQL_CB_CLOSE, which is the ANSI-specified default behavior, means that the cursor is closed after a commit or rollback. As a result, an MFC recordset will not have a valid cursor after CommitTrans() or Rollback() is called. No operations should be performed on a recordset prior to calling Requery(). An application may want to preserve cursors across transaction commits and rollbacks. The default behavior can be overridden for the SQL Server ODBC driver by using a driver-specific connection option, SQL_PRESERVE_CURSORS. This option can be set by calling SQLSetConnectOption(), as shown below:
CMyDatabase db; db.Open( NULL, FALSE, FALSE,"ODBC;", FALSE ); // Select SQL Server // 6.x data source // rcode will equal 0 (SQL_SUCCESS) if call succeeds int rcode = ::SQLSetConnectOption (db.m_hdbc,1204, 1L ); // 1204 = SQL_PRESERVE_CURSORS, 1L = SQL_PC_ON // values for driver-specific options are given in the driver help file // if NOT using MFC 4.2, need to enable transaction support // look at Q128208 for more information // db.SetTransactions(); BOOL bCanTrans = db.CanTransact(); if (bCanTrans) { CMyRecordset rs(&db); BOOL ret = db.BeginTrans(); rs.Open( CRecordset::dynaset ); rs.Edit(); // change some fields rs.Update(); db.Rollback(); // since the cursor is preserved, we do NOT have // to call Requery rs.MoveFirst(); }Note that SQL_PRESERVE_CURSORS works only for server-side cursors. As a result, even after setting this connection option, the ODBC driver will return SQL_CB_CLOSE when queried for SQL_CURSOR_COMMIT_BEHAVIOR. For additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q138280 TITLE : INF: SQLGetInfo Returned with SQL_PRESERVE_CURSORS REFERENCESMFC Technote #47 - "Relaxing Database Transaction Requirements." MFC Technote #68 - "Performing Transactions with the Microsoft Access 7 ODBC Driver" MFC Encyclopedia articles:
Keywords : MfcDatabase kbprg kbusage Version : WINDOWS NT: 2.0, 2.1, 2.2, 4.0, 4.1, 4.2, 5.0 Platform : NT WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |