Add a reference to the System.Transactions assembly.
 | Distributed transaction .net 2.0 |
| using System;
| | 1:using System.Data;
| | 2:using System.Data.SqlClient;
| | 3:using System.Transactions;
| | 4:
| | 5:namespace Arindam.DistributedTransaction
| | 6:{
| | 7: public class Distributed_Transaction_using_system_transaction
| | 8: {
| | 9: void DistributeData()
| | 10: {
| | 11:
| | 12: TransactionOptions options = new TransactionOptions();
| | 13: options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
| | 14: options.Timeout = new TimeSpan(0, 2, 0);
| | 15:
| | 16: using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
| | 17: {
| | 18: using (SqlConnection connA = new SqlConnection(connStringA))
| | 19: {
| | 20: using (SqlCommand cmdA = new SqlCommand(sqlStmtA, connA))
| | 21: {
| | 22: int rowsAffectedA = cmdA.ExecuteNonQuery();
| | 23: if (rowsAffectedA > 0)
| | 24: {
| | 25: using (SqlConnection connB = new SqlConnection(connStringB))
| | 26: {
| | 27: using (SqlCommand cmdB = new SqlCommand(sqlStmtB, connB))
| | 28: {
| | 29: int rowsAffectedB = cmdB.ExecuteNonQuery();
| | 30: if (rowsAffectedB > 0)
| | 31: {
| | 32: transactionScope.Complete();
| | 33: }
| | 34:
| | 35: } // Dispose the second command object.
| | 36:
| | 37: } // Dispose (close) the second connection.
| | 38:
| | 39: }
| | 40:
| | 41: } // Dispose the first command object.
| | 42:
| | 43: } // Dispose (close) the first connection.
| | 44:
| | 45: } // Dispose TransactionScope object, to commit or rollback transaction
| | 46:
| | 47: }
| | 48: }
| | 49:}
| | 50: |
|