Re: Lock MSSQL Table Row?
- From: xenophon <xenophon@xxxxxxxxxxxxx>
- Date: Thu, 20 Oct 2005 00:20:24 -0400
I am using OleDb to connect to MSSQL. I am also using it to connect to
PostgreSql and Access 2003 and Visual FoxPro 9.0.
In my data store (let's just use MSSQL 2000 in this example), I have a
parent table with a "lock" column containing a GUID. The application
reads the parent table and 2 child tables into a DataSet. Data is
modified in all DataTables in the DataSet.
When a save happens, I want to lock the parent row, compare the "lock"
column in the data store to the "lock" column of the DataSet, and if
there is a match make changes in the parent and all children, change
the "lock" column in the dataset to reflect the change, and then
issue an "unlock". If another user/thread attempts to do the same
save, they are blocked until the lock on the parent table is released,
and then their save will fail because the "lock"column does not match
the DataSet "lock" column.
So....
Are you saying that I can do this:
1. Open OleDbConnection to my data store.
2. Create OleDbCommand and set connection.
3. Set OleDbCommand text to "select * from parenttable where...."
4. Do OleDbTransaction lockTrans = connection.BeginTransaction();
5. Do lockTrans.IsolationLevel = IsolationLevel.Serializable;
6. Do OleDbCommand.ExecuteScalar(), which will lock the row of the
parent table.
7. Use the existing OleDbConnection with new OleDbCommands to do
updating from the DataSet/DataTables->database.
8. Use the old OleDbCommand to "update parenttable where...." to set
the "lock" column GUID to something new.
9. Close the connection, which will unblock the database parent row
from others trying to update?
Please let me know, this is very interesting.
The VS.NET 2003 help says:
" A range lock is placed on the System.Data.DataSet, preventing other
users from updating or inserting rows into the dataset until the
transaction is complete. "
I'm not using a DataSet as far as locking is concerned, am I?
Thanks.
On Tue, 04 Oct 2005 08:36:47 GMT, v-kevy@xxxxxxxxxxxxxxxxxxxx (Kevin
Yu [MSFT]) wrote:
>Hi xenophon,
>
>First of all, I would like to confirm my understanding of your issue. From
>your description, I understand that you need to lock a certain row that
>one user is modifying on it. If there is any misunderstanding, please feel
>free to let me know.
>
>I suggest you try to use SqlConnection.BeginTransaction to start a
>transaction, and set its IsolationLevel to Serializable. It will place a
>range lock on the resultset, preventing other users from updating or
>inserting rows into the dataset until the transaction is complete. Here is
>a link for your reference.
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
>frlrfsystemdatasqlclientsqltransactionclassisolationleveltopic.asp
>
>Kevin Yu
>=======
>"This posting is provided "AS IS" with no warranties, and confers no
>rights."
>
.
- Follow-Ups:
- Re: Lock MSSQL Table Row?
- From: Kevin Yu [MSFT]
- Re: Lock MSSQL Table Row?
- References:
- RE: Lock MSSQL Table Row?
- From: Kevin Yu [MSFT]
- RE: Lock MSSQL Table Row?
- Prev by Date: Re: Question about-> Keyword not supported: '"database'.
- Next by Date: Re: OleDb Provider for Excel Not Reading Properly
- Previous by thread: RE: Lock MSSQL Table Row?
- Next by thread: Re: Lock MSSQL Table Row?
- Index(es):
Relevant Pages
|