使用事务处理SQL数据时,遇到Exception:The transaction manager has disabled its support for remote/network transactions

本地是.net开发的Console Application, 利用Entity Framework 访问数据库,数据库在另一个Server机器上,使用SQL Server 2005。在本地调试代码,连接远程数据库,使用事务处理数据插入的问题时,遇到Exception。部分代码如下:

try
{
	using (TransactionScope scope = new TransactionScope())
        {
                    this.dbEntites.ResultInfoes.AddObject(info);
                    this.dbEntites.SaveChanges();

                    result.Id = info.Id;
                    this.dbEntites.Results.AddObject(result);
                    this.dbEntites.SaveChanges();

                    scope.Complete();
        }
}
catch (Exception ex)
{
       Trace.WriteLine("Cannot add result into database.");
       Trace.WriteLine(ex);
}

 

在执行第一次的SaveChanges时,没有问题,但在执行第二次的SaveChanges时,Catch到Exception。如图:

trandisabled

 

主要的错误是这里: {"The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)"}

从网上查了一下,主要是因为网络在访问MSTDC时,有限制,默认是不允许通过网络访问的。解决办法如下:

  1. 确认"Distribute Transaction Coordinator"服务在Server和本地都是运行状态。
  2. 在Server上打开 Component Services。 Control Panel –> System and Security –> Administrative Tools –> Component Services.
  3. 展开至 Component Services –> Computers –> My Computer –> Distributed Transaction Coordinator –> Local DTC, 右键,选择Properties。在弹出的Dialog中选择Tab “Security”, 勾选Network DTC Access 等选项,具体设置如下图。
    trandisabled-1
  4. 确认后会要求重新启动Service。注:如果在后面的步骤做完后,还是无法解决问题,可能需要重启电脑。
  5. 确认DTC不会被你的防火墙阻止。
  6. 重复以上2-5 在你本地(Client)也执行一遍。

再次运行程序,问题解决。如果还有问题,一是重启电脑,二是检查SQL Server中的DTC是否开启。

posted on 2011-09-22 12:01  海毛虫  阅读(728)  评论(0编辑  收藏  举报