BeginTransaction 未调用 BEGIN TRANSACTION
我有一段使用 BeginTransaction()
的简单代码。生成的事务被分配给我用于某些 sql 命令的连接。
当我分析生成的 sql 时,我在任何时候都看不到 BEGIN TRANSACTION
。可能发生什么情况会阻止交易被使用?
I've got a simple bit of code that uses BeginTransaction()
. The resulting transaction is assigned to the connection that I'm using for some sql commands.
When I profile the resulting sql, I don't see a BEGIN TRANSACTION
at any point. What might be happening that would prevent the transaction from being used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ADO.NET 时,事务在较低级别进行处理。没有“BEGIN TRANSACTION”语句发送到服务器。
Transactions are handled at a lower level when using ADO.NET. There are no "BEGIN TRANSACTION" statements sent to the server.
您需要确保不仅在连接对象上设置事务,还需要将事务分配到 sqlCommand 中。
有关示例,请参阅这篇 codeproject 文章。
You need to ensure that you not only set the transaction on the connection object, but you also need to assign the transaction into the sqlCommand.
See this codeproject article for an example.
重申 Philippe 的声明:
使用 ADO.NET 时,事务是在较低级别处理的。没有“BEGIN TRANSACTION”语句发送到服务器。
在某些时候,SQL 必须转换为实际的调用。大多数 ADO.NET(我使用过的所有 ADO.NET)经常发送数据库特定命令来 BEGIN、COMMIT 和 ROLLBACK 事务,因为发送 ASCII(或其他任何内容)的效率低于服务器必须解析的内容。
这就是为什么发送参数化查询通常比基于纯 SQL 的查询更快,因为库可以发送特定命令,从而减少解析并可能减少数据验证(?)。
哈!
To reiterate Philippe's statement:
Transactions are handled at a lower level when using ADO.NET. There are no "BEGIN TRANSACTION" statements sent to the server.
At some point SQL has to be converted into actual calls. Most ADO.NET (all that I've worked with) often send a database specific command to BEGIN, COMMIT, and ROLLBACK transactions as sending ASCII (or whatever else) would be less efficient than something the server will have to parse.
This is why sending parameterised queries are often faster than pure SQL based ones as the library can send specific commands which results in less parsing and probably less data validation (?).
HTH!