gpt4 book ai didi

ado.net - 在 TransactionScope 中使用 ADO.Net 事务?

转载 作者:行者123 更新时间:2023-12-02 17:06:50 26 4
gpt4 key购买 nike

我一直在阅读有关 TransactionScope 的内容,并对它与 ADO.Net 事务的互操作性有疑问。现在我们有一些数据访问方法,每个方法都调用存储过程并开始和提交自己的单独事务。简单明了,样板的东西,方法如下所示:

sqlCommand = //create SQLCommand with Stored proc
SqlTransaction myTransaction = myConnection.BeginTransaction();
sqlCommand.Transaction = sqlTransaction;
sqlCommand.Execute();
sqlTransaction.Commit();

我已经简化了一些事情,但你明白了。

我现在需要连续调用其中两个类方法,并从我的业务层作为一个团队提交或回滚它们。我无法修改数据方法,因此我正在考虑将这两个调用放在 TransactionScope block 中。

如果我这样做,在创建 TransactionScope 时应该使用什么类型的参数?我已经使用 TransactionScopeOption.RequiresNew 选项尝试过此操作,并且似乎有效,但这只是我的实验,我不确定这是否是可行的方法。 (我会在这里指出,这些是专门的 SQL 事务,在同一 SQL 服务器上运行。)

我看到 TransactionScope 有构造函数选项来处理 COM+ 事务。因为我使用的是 ADO.Net 事务,所以这与这里相关吗?感谢您的建议。

最佳答案

TransactionScope 文档 here是一个很好的起点。

基本上,它使得在 ADO 级别处理事务变得不必要。连接将自动考虑现有的环境事务(默认)。

请记住,您还可以通过连接字符串更改连接在环境事务中登记的方式。这个link将提供更多详细信息。

Enlist 'true'. true indicates that the SQL Server connection pooler automatically enlists the connection in the creation thread's current transaction context.

关于ado.net - 在 TransactionScope 中使用 ADO.Net 事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1786591/

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