我想在 SQL Server 中锁定一个表给定的时间。我在代码级别使用 C#。我该如何实现呢,我还需要验证表是否被锁定。我对 SQL Server 中的锁定表了解不多。我想使用实体框架来实现它。任何帮助深表感谢。
问问题
637 次
1 回答
2
您可以尝试如下所示。
using (var context = new YourContext())
{
using (var dbContextTransaction = context.Database.BeginTransaction())
{
//Lock the table during this transaction
context.Database.ExecuteSqlCommand("SELECT 1 FROM YourTable WITH (TABLOCKX)
WAITFOR DELAY '00:03:00'");
//your work here
dbContextTransaction.Commit();
}
}
注意:BeginTransaction
表锁在退出块 后会被释放
于 2016-10-18T04:35:50.247 回答