执行 SQLCommand 而不指定事务

发布于 2024-07-25 13:18:25 字数 205 浏览 1 评论 0原文

我们的应用程序中通过 SqlCommand 在 SQL Server 数据库上执行 SELECT 查询来获取一些数据列表。 我们没有在 SqlCommand 上显式设置事务,而是只是向其传递 SqlConnection 并运行它。 当没有指定事务时,SQL Server 是否会启动并使用默认 IsolationLevel 为 ReadCommited 的默认事务?

We have some lists of data being fetched in our application via a SqlCommand performing a SELECT query on a SQL Server database. We do not explicitly setup a transaction on the SqlCommand, instead just passing it a SqlConnection and running it. Is it the case that when no transaction is specified that SQL Server will initiate and use a default transaction with the default IsolationLevel of ReadCommitted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-08-01 13:18:25

SQL 会为您的语句隐式创建一个事务,并且该事务会在语句完成时提交。 该事务的隔离级别将是当前隔离级别,默认为 READ COMMITTED。 某些语句会覆盖当前的隔离级别并强制执行 READ COMMITTED(例如 RECEIVE)。

如果您的 SqlCommand 执行批处理(更多语句),则访问表的每个语句都将创建自己的事务。

默认的事务自动提交是通过更改 SET IMPLICIT_TRANSACTION ON 来控制的。

有关详细信息,请参阅控制事务

SQL creates a transaction implicitly for your statements, and this transaction is committed when the statement completes. The isolation level of this transaction will be the current isolation level, which defaults to READ COMMITTED. Certain statements overwrite the current isolation level and enforce READ COMMITTED (eg. RECEIVE).

If your SqlCommand executes a batch (more statements) then each statement that access tables will create its own transaction.

The default autocommit of transactions is controlled by changing the SET IMPLICIT_TRANSACTION ON.

See Controlling Transactions for more details.

断舍离 2024-08-01 13:18:25

SQL Server 无需显式事务即可正常工作。 但是,是的,我相信它本质上是读提交的(当然,除非您向查询对象添加额外的提示,例如 UPDLOCK / NOLOCK)。 您可以使用以下命令进行调查:

DBCC USEROPTIONS

其中显示(除其他外):

isolation level read committed

SQL Server can work happily without an explicit transaction. But yes, I believe it is essentially read-committed (unless, of course, you add extra hints to your query objects,such as UPDLOCK / NOLOCK). You can investigate this with:

DBCC USEROPTIONS

which shows (among others):

isolation level read committed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文