开发者

Prevent insert data into table at the same time

开发者 https://www.devze.com 2023-02-03 15:01 出处:网络
I\'m working on a online sales web site. I\'m using C# 4,0 and SQL server 2008 and I want to controland prevent users simultaneously insert into the table like dbo.orders... How c开发者_StackOverflow中

I'm working on a online sales web site. I'm using C# 4,0 and SQL server 2008 and I want to control and prevent users simultaneously insert into the table like dbo.orders... How c开发者_StackOverflow中文版an I do that?


Inserts will not be a problem, but updates can be. The term that you need to research is database concurrency. There are four basic models you can implement each with its own pros and cons. Some are better suited for certain situations and there are hundreds of articles on the web for this subject.


Are you trying to solve this in C# code on in SQL? Because in SQL it's simple. If you add BEGIN TRAN in the beginning of the stored procedure and COMMIT in the end, this will act as a lock in C# preventing concurrent code executions effectively serializing the requests. So if there are two inserts, they will be executed one after another. One thing to remember is that it will be blocking operation, i.e. the second insert won't start until the first one is finished (regardless successfully or not).


In your Add method you can use Locking with lock keyword, this will allow one thread at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消