C# method to lock SQL Server table

前端 未结 4 1725
旧时难觅i
旧时难觅i 2021-02-14 03:29

I have a C# program that needs to perform a group of mass updates (20k+) to a SQL Server table. Since other users can update these records one at a time via an intranet website,

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-14 03:41

    Try this: when you get records from you table (in the GetRecords() function?) use TABLOCKX hint:

        SELECT * FROM Table1 (TABLOCKX)
    

    It will queue all other reads and updates outside your transaction until the transaction is either commited or rolled back.

提交回复
热议问题