加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – “事务已在事务范围内隐式或显式提交或中止”

发布时间:2020-12-15 21:25:22 所属栏目:百科 来源:网络整理
导读:我在两个不同的网络上有一个Web服务器和两个DB服务器,Db1和Db2(远程数据库). DB1:SQL Server 2008 R2,操作系统:Windows Server 2003 SP2 DB2:SQL Server 2000,操作系统:Windows Server 2003 R2 Web服务器:Windows Server 2003 R2 我想在这些数据库中插
我在两个不同的网络上有一个Web服务器和两个DB服务器,Db1和Db2(远程数据库).

> DB1:SQL Server 2008 R2,操作系统:Windows Server 2003 SP2
> DB2:SQL Server 2000,操作系统:Windows Server 2003 R2
> Web服务器:Windows Server 2003 R2

我想在这些数据库中插入两个不同的记录,我正在使用TransactionScope.

using (TransactionScope tscope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    //open connection db 1
    //insert into db1

    //open connection db2 -- the problem is here
    //insert into db2

    tscope.Complete();
}

当我跟踪源代码时,插入第一个数据库成功完成但是当第二个连接想要打开时我遇到下面的错误.

错误:

The transaction has already been implicitly or explicitly committed or aborted.

我已经配置了MSDTC,Distributrd事务协调器,一切都还可以.我可以在我的数据库服务器中分配事务,我没有问题. TransactionScope出了什么问题?请帮我.

第一个连接是LINQ TO SQL:

DataClassesDataContext Dac = new DataClassesDataContext();
Dac.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionCs"].ConnectionString;
tbl_KargahDistribution Workshop = new tbl_KargahDistribution();
Workshop.WpCode = Bodu.WpRealCode;
Workshop.State = Bodu.BduState;
Workshop.City = Bodu.BduCity;
Workshop.Town = Bodu.BduTown;
Workshop.SubSystem = Bodu.BduSubSystem;
Dac.tbl_KargahDistributions.InsertOnSubmit(Workshop);
Dac.SubmitChanges();

第二个连接是:

Queries Qu = new Queries();
SqlCon = new SqlConnection(BoBaz.BazConnectionString);
SqlCon.Open();
string sq = Qu.InsertWorkshopBaseInfo();
SqlCom = new SqlCommand(sq,SqlCon);

解决方法

Dac.SubmitChanges();

原因

The transaction has already been implicitly or explicitly committed or aborted.

见How to use transactions with a datacontext

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读