How to use SqlTransaction in .net 2.0 so that when I start reading data from a table, that table is blocked for others (other programs) to read/write to that table?
If SqlTransaction is not a good option, than what is?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should be allowed by using Serializable transaction together with
TABLOCKXhint in initial select statement.TABLOCKXshould take exclusive lock on the table so nobody else can use that table and Serializable transaction should demandHOLDLOCKwhich means that all locks are kept until end of the transaction (you can useHOLDLOCKdirectly).Be aware that such approach can be big bottleneck because only one transaction can operate on such table = no concurrency. Even if you read and work with single record from million nobody else will be able to work with the table until your transaction ends.
So the command should look like:
To use serializable transaction you can use
SqlTransaction:Or
System.Transactions.TransactionScope(default isolation level should be Serializable).