2012-10-24 24 views
0

我有一種方法可以達到2種方法。一個處理插入,另一個處理更新。我把這兩種方法的事務範圍放在一起,所以如果有任何問題它會回滾。我注意到它將數據放入數據庫中,但出錯時它並未將其刪除。我已經爲範圍嘗試了RequiresNew和Required選項,但這似乎沒有什麼區別。TransactionScope無法正常使用2層數據庫設置

附加信息: 這些存儲過程實際上存儲在一個「工具」數據庫中,該數據庫持有存儲過程,但實際上它們正在修改不同數據庫中的記錄。

該方法將在40個不同的表中插入大約700多條記錄。

主要方法

using (var scope = new TransactionScope(TransactionScopeOption.Required)) 
{ 
    foreach (var change in changes) 
    { 
     switch (change.ChangeType) 
     { 
      case ChangeTypeEnum.Insert: 
       var result = DataAccess.InsertTableRow(sourceEnvironmentId, SuperClientVendorID, 
                 DatabaseName, DataRouteName, change, 
                 typeNamespace); 
       postList.Add(result); 
       break; 
      case ChangeTypeEnum.Update: 
       DataAccess.UpdateTableRow(sourceEnvironmentId, SuperClientVendorID, DatabaseName, 
              DataRouteName, change, typeNamespace); 
       postList.Add(change); 
       break; 
     } 
    } 

    scope.Complete(); 
} 

插入方法

var sproc = string.Format("Carma.usp_{0}_{1}_ins", databaseName, managedState.TypeName); 
var connString = DataAccessManager.GetConnectionString(executionEnvironment, superClientVendorID, routeName, sproc, false); 

using (var newConnection = new SqlConnection(connString)) 
{ 
    newConnection.Open(); 

    using (var newSqlCommand = new SqlCommand(sproc, newConnection)) 
    { 
     newSqlCommand.CommandType = CommandType.StoredProcedure; 
     newSqlCommand.CommandTimeout = Setup.TimeOut; 
     newSqlCommand.Parameters.AddRange(GetParameters(managedState, typeNamespace, true)); 

     newSqlCommand.ExecuteNonQuery(); 

     return managedState; 
    } 
} 
+0

'他們實際上是修改記錄的是他們這裏的關鍵,我相信。其他數據庫_是需要在事務中註冊的數據庫,它不會。 – Oded

+0

我在想這可能是問題所在。有沒有辦法註冊其他數據庫?我有一種感覺,這將是一個失敗。 – Tony

+0

不是直接的,也不是使用中間數據庫的「跳躍」。您可能能夠按照計劃對中介數據庫進行必要的更改(因此,只有在整個批次成功後纔會有需要轉移的內容),但我不知道應用程序要求如何難以推薦任何東西。 – Oded

回答

0

所以同事能夠..幫助我這這裏是修改後的代碼希望這會幫助別人那裏:)

調用方法:

using (var scope = new TransactionScopeMS()) 
{ 
    foreach (var change in changes) 
    { 
     switch (change.ChangeType) 
     { 
      case ChangeTypeEnum.Insert: 
        var result = DataAccess.InsertTableRow(sourceEnvironmentId, SuperClientVendorID, 
                   DatabaseName, DataRouteName, change, 
                   typeNamespace); 
        postList.Add(result); 
        break; 
      case ChangeTypeEnum.Update: 
        DataAccess.UpdateTableRow(sourceEnvironmentId, SuperClientVendorID, DatabaseName, 
                DataRouteName, change, typeNamespace); 
        postList.Add(change); 
        break; 
     } 
    } 

    scope.Complete(); 
} 

插入方法:

不同database`
using (IDbConnection newConnection = TransactionScopeMS.Current.GetConnection(connectionString)) 
     { 
      using (IDbCommand newSqlCommand = newConnection.CreateCommand(sproc, CommandType.StoredProcedure, TransactionScopeMS.Current.GetTransaction(connectionString))) 
      { 

       foreach (var pram in GetParameters(managedState, typeNamespace, true)) 
       { 
        newSqlCommand.Parameters.Add(pram); 
       } 

       newSqlCommand.ExecuteNonQuery(); 

       return managedState; 
      } 
     }