Microsoft KB Archive/288054

= SAMPLE: RLockMFC.exe Demonstrates Row-Level Locking Using MFC and SQL Server =

Article ID: 288054

Article Last Modified on 8/5/2004

-

APPLIES TO


 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition
 * Microsoft ODBC Driver for Microsoft SQL Server 3.7
 * Microsoft ODBC Driver for Microsoft SQL Server 3.7
 * Microsoft Data Access Components 2.5
 * Microsoft Data Access Components 2.5 Service Pack 1
 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q288054



SUMMARY
The RLockMFC.exe sample program demonstrates how to use the Microsoft Foundation Class (MFC) CDatabase and CRecordset objects with SQL Server in a Visual C++ application to pessimistically lock individual rows within a recordset while they are being changed.



MORE INFORMATION
The following file is available for download from the Microsoft Download Center:

RLockMFC.exe

Release Date: April 9, 2001

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. The sample opens a CRecordset object with a SQL Server table. It allows a client to browse the records and update the name field if required. If a second client connects, they can browse the recordset and edit any of the records except the one that is being edited by the first client. If both clients try to edit the same record, a message box with the following messages is displayed

&quot;State:37000,Native:1222,Origin:[Microsoft][ODBC SQL Server Driver][SQL Server]&quot;

&quot;Lock request time out period exceeded.&quot;

Followed by this message:

&quot;The Row is already exclusively locked by some other user&quot;

Clients can browse the recordset by using the navigation buttons. Click the Lock The Row button to start editing a record and then click either Cancel Save or Save The Change to discard or save your changes respectively.

NOTE: Clicking any navigation button during a transaction will roll back any changes.

To run the sample, follow these steps:   Create the following table in SQL Server: CREATE TABLE [dbo].[RowLock] (   [pk] [int] IDENTITY (1, 1) NOT NULL,    [name] [char] (50) NULL ) ON [PRIMARY] GO

ALTER TABLE [dbo].[RowLock] WITH NOCHECK ADD CONSTRAINT [PK_RowLock] PRIMARY KEY CLUSTERED (       [pk]    )  ON [PRIMARY] GO  Insert some data into the RowLock table. Run two instances of the sample and browse the recordset using both clients. Browse to a record and click the Lock The Row button to start editing with the first client. Then try to edit the same record with the second client. A message box appears, which states that the row is currently locked.

The sample works by opening an optimistic server-side cursor on the recordset; this allows free browsing of the recordset. When the user begins editing, a pessimistic cursor opens on the recordset at the appropriate record, locking only that record. When the user selects either Cancel Save or Save The Change, it releases the pessimistic lock and opens an optimistic cursor again.