Cursors and Transactions, Data Entry and Concurrency

ODBC and SQL Server

ODBC and SQL Server

Cursors and Transactions, Data Entry and Concurrency

The cursor sample illustrates using Microsoft® SQL Server™ cursors to allow concurrent access to data on a server running an instance of SQL Server. The sample shows three methods of concurrent data access:

  • Optimistic, using SQL Server timestamp data types.

  • Pessimistic, using extremely isolated transactions and row-level locking.

  • Pessimistic, using the Microsoft Distributed Transaction Coordinator (MS DTC).

A dialog box, available when no document windows are open, allows the user to select the method of concurrency control to be used by the application.

The sample relies on data shipped with the ODBC 3.x samples. The data can be installed using the LoadData sample and the Lddist.cmd file. For more information about Lddist.cmd, see ODBC Samples.

The sample is a Microsoft Foundation Class MDI application. Each MDI window contains a property sheet of customer data from a fictitious bank. The window is implemented on a separate connection to the database so that a single user can experiment with various concurrency handling methods from a single instance of the application.

Customers at the bank can have one or two accounts and you can either deposit or withdraw funds from a customer's checking or savings account. Each transaction inserts a row into an activity file and then attempts a positioned update of a balance item for the account. If the balancing transaction fails, the activity file insert is rolled back.

To open a connection, select File\New from the menu.

Functions Illustrated
SQLAllocHandle SQLDriverConnect SQLGetDiagRec
SQLBindParameter SQLEndTran SQLNumResultCols
SQLCloseCursor SQLExecDirect SQLSetConnectAttr
SQLColAttribute SQLFetch SQLSetEnvAttr
SQLDataSources SQLFreeHandle SQLSetPos
SQLDescribeCol SQLGetCursorName SQLSetStmtAttr
SQLDisconnect SQLGetDiagField