Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 事务处理镜回滚_C#_Transactions_Transactionscope - Fatal编程技术网

C# 事务处理镜回滚

C# 事务处理镜回滚,c#,transactions,transactionscope,C#,Transactions,Transactionscope,我在一个主方法中有一个方法。如果父方法失败,我需要子方法能够回滚。这两个数据连接使用不同的服务器。在我添加事务作用域之前,它们工作得很好。但当我将它们绑定在一起时,child方法中止 编辑:错误消息:已禁用分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具为MSDTC的安全配置中的网络访问启用DTC public static void LoopStudent() { try { using(TransactionScope scope = new Transa

我在一个主方法中有一个方法。如果父方法失败,我需要子方法能够回滚。这两个数据连接使用不同的服务器。在我添加事务作用域之前,它们工作得很好。但当我将它们绑定在一起时,child方法中止

编辑:错误消息:已禁用分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具为MSDTC的安全配置中的网络访问启用DTC

public static void LoopStudent() 
{
  try 
  {
    using(TransactionScope scope = new TransactionScope()) 
    {
      String connString = ConfigurationManager.AppSettings["DBConnection"];
      using(SqlConnection webConn = new SqlConnection(connString)) 
      {
        webConn.Open();
        String sql = "select * from students";
        using(SqlCommand webComm = new SqlCommand(sql, webConn)) 
        {
          using(SqlDataReader webReader = webComm.ExecuteReader()) 
          {

            if (webReader.HasRows) 
            {
              while (webReader.Read()) 
              {
                int i = GetNextId();
              }
            } 
            else 
              Console.WriteLine("wrong");
          }
        }
      }

      scope.Complete();
    }
  }
  catch (Exception ex) 
  {
    Console.WriteLine("Error " + ex.Message);
  }

} //End LoopThroughCart         

public static int GetNextId(String str) 
{
  int nextId = 0;
  String connString = ConfigurationManager.AppSettings["SecondDBConnection"];
  try 
  {
    using(TransactionScope scope = new TransactionScope()) 
    {
      using(SqlConnection webConn = new SqlConnection(connString)) 
      {
        webConn.Open();
        using(SqlCommand webComm = new SqlCommand("GetNextId", webConn)) 
        {
          //do things
        }
      }
      scope.Complete();
    }
  } 
  catch (TransactionAbortedException ex) 
  {
    Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
  } 
  catch (ApplicationException ex) 
  {
    Console.WriteLine("ApplicationException Message: {0}", ex.Message);
  }
  return nextId;
} //End GetNextId

如果不在内部方法中使用RequireRenew,则如果父级未能提交事务,内部方法将自动回滚


您遇到了什么错误?

我在问题中添加了错误消息。我认为你所说的只适用于相同的联系?我正在连接两台服务器。请转到“运行”并键入“msdtc”。现在,导航到“组件服务->计算机->我的计算机>分布式事务协调器”,右键单击“本地DTC”并选择“属性”。打开“安全”选项卡。在这里您可以看到DTC选项,甚至是“网络”选项。如果这有帮助,别忘了将其标记为帮助他人的答案。