TransactionScope还原存储过程更改

问题描述 投票:0回答:2

我在存储过程中使用事务范围,在存储过程中我们不使用事务,但是在完成事务范围之后,将还原在存储过程中所做的更改,无法理解是什么问题,下面是示例代码,

    using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionOptionConstant.option1)) 
{

    //Call Stored Procedure 

    transactionScope.Complete();

    }
c# sql-server asp.net-web-api stored-procedures transactionscope
2个回答
0
投票

您使用选项TransactionScopeOption.RequiresNew创建范围。因此,您的SP可以在不同的事务中执行。

尝试设置选项TransactionScopeOption.Required


0
投票

[如果没有看到实际的代码(存储过程,调用它的代码以及周围的其余代码,很难得到调试。我的一些提示。

使用try catch包围代码

try {
    using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionOptionConstant.option1)) 
    {
        //Call Stored Procedure 
        transactionScope.Complete();
    }
} catch (Exception ex) {
   // Make sure you never reach this when you call complete.
}

Check the following from Microsoft docs

如果TransactionScope对象创建了事务,则资源管理器之间的实际提交工作将在End Using语句处发生。

如果没有使用退出,则不会发生提交。

检查您的存储过程

也许您的存储过程实际上并未保存更改。这也通过注释添加,但是很可能是它在sql方面捕获了一些异常,该异常在那里处理并没有传播回代码。

© www.soinside.com 2019 - 2024. All rights reserved.