gpt4 book ai didi

linq-to-sql - 使用 Linq to SQL 进行 NOLOCK

转载 作者:行者123 更新时间:2023-12-03 05:25:12 24 4
gpt4 key购买 nike

是否可以让 Linq2Sql 在其 SQL 中发出 NOLOCK?如果是这样,怎么办?

最佳答案

是的,所以这里是条目 from my blog :

The NOLOCK hint is essentially the same as wrapping a query in a transaction whose "isolation level" is set to "read uncommitted". It means that the query doesn't care if stuff is in the process of being written to the rows it's reading from - it'll read that "dirty" data and return it as part of the result set.

Turns out that you can do the whole "read uncommitted" transaction thing using the old System.Transactions namespace introduced in .NET 2.0. Here's some sample code:

using (var txn = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadUncommitted
}
))
{
// Your LINQ to SQL query goes here
}

So I'm creating a new TransactionScope object and telling it to use a read-uncommitted isolation level. The query within the "using" statement now acts as if all its tables were reading with the NOLOCK hint.

以下是 Google 搜索“linq sql nolock”的第一个结果:

InfoQ: Implementing NOLOCK with LINQ to SQL and LINQ to Entities

Matt Hamilton - LINQ to SQL and NOLOCK Hints : Mad Props!

Scott Hanselman's Computer Zen - Getting LINQ to SQL and LINQ to ...

关于linq-to-sql - 使用 Linq to SQL 进行 NOLOCK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1220807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com