Question

I want to lock a record to prevent nobody can update while I using.

but after lock record, even myself can not update the locked record :(

Do I have to unlock before I update the record?

or is there a way to update the record that I locked myself?

string query = "SELECT * FROM table1";
AdsCommand cmd = conn.CreateCommand();
cmd.CommandText = query;

AdsExtendedReader reader = cmd.ExecuteExtendedReader();
reader.Read();

int recordNo = reader.RecordNumber;
reader.LockRecord(recordNo);

// do something with table1 record
// while doing something, I need to lock the record before I update record

AdsCommand cm = new AdsCommand("UPDATE table1 SET field1 = 'UPDATED'", conn);
cm.ExecuteNonQuery();
Was it helpful?

Solution

It is not possible to share a record lock between a table that is opened "directly" (as opposed to through an SQL operation) and SQL operations or other table instances.

One possibility would be to do the update through the extended reader after the lock is obtained. You could use SetString or SetValue.

reader.SetString( colNumber, 'UPDATED' );

Another possibility might be to update the record in a transaction. Then that record will be kept locked by that user until the transaction is committed. For example, you could use something like the following statement as a "no-op" (assuming no triggers are involved) to lock a record in a transaction without modifying the data:

UPDATE table1 set field1 = field1 WHERE pk=1;

OTHER TIPS

You can't lock it and then update it using a different SQL statement; that opens another instance of the query, which can't happen because you locked the table with the first one. Just don't worry about locking it, and run your UPDATE statement. ADS will lock the record, make the change, and unlock it for you automatically.

// Note I added a WHERE statement with a dummy value for `finvno`, since your 
// select didn't provide one 
AdsCommand cm = new AdsCommand("UPDATE table1 SET field1 = 'UPDATED' WHERE finvno = 'SomeInvNo'", conn);
cm.ExecuteNonQuery();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top