在C#代码中使用事务范围对象 [英] Using Transaction scope object in C# code
问题描述
Hello All,
Hello All,
我正在尝试在C#中使用transactionscope对象。让我解释一下我真正想要的场景。
I am trying to use transactionscope object in C#. Let me explain the scenario of what I really want.
我试图插入到同一个数据库中的多个表中,我想把它作为单个逻辑单元包含在内执行其他任何操作都不应该执行。
I am trying to insert in to multiple tables in the same db and I want to enclose this as a single logical unit as either all should be performed else none should be performed.
例如,考虑需要插入的表是factoryemployee,factoryConsultant,EmpSalary,EmpSpecialAllowance以及所有作为单独的数据层,业务层。
For example consider tables needed to be inserted are factoryemployee, factoryConsultant, EmpSalary, EmpSpecialAllowance and all as separate data layer, business layer.
我正在使用aspx.cs类中的事务(我知道这不是正确的做法)但是我收到的错误是说事务中止或有时这不是有效的事务对象。下面是我正在使用的一段代码
I am using the transaction from aspx.cs class (I know that is not the correct way of doing) but I am getting error saying transaction is aborted or sometimes this is not valid transaction object. Below is piece of code which i am using
使用(TransactionScope scope = new TransactionScope()){
using(TransactionScope scope = new TransactionScope()){
factoryConsultant.update();
factoryConsultant.update();
EmpSalary.Insert();
EmpSalary.Insert();
EmpSpecialAllowance .Insert() ;
EmpSpecialAllowance .Insert();
scope.complete();
scope.complete();
}
任何人,请帮我解决这个问题情况。
Anybody, please help me how to handle this situation.
谢谢,
推荐答案
您的数据库连接应在交易范围内打开和关闭。
Your database connection should open and close within the transaction scope.
这篇关于在C#代码中使用事务范围对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!