1

Sqltransaction

I am trying to insert in to tables in the multiple db and I want to one of them is not execute ,all stetement is rooleback. i want use sql transaction but i dont know how to use sql transaction and transactionscope in c# i transaction scope

   try
    {
     using (tScope = new TransactionScope(TransactionScopeOption.Required,TOption))
      {
        
      ConAcnt.open();
     SqlCommand Com1 = new SqlCommand("INSERT",ConAcnt);
      Com1.ExecuteNonQuery();
      
      ConBase.open();
     Com2 = new SqlCommand("INSERT",ConBase);   
      Com2.ExecuteNonQuery();
       tScope.Complete();
      }

 catch (Exception ex)
{
    tScope.Dispose();                            
                                                           
}


   }  

i want when the com2 not execute,com1 is rooleback,but commit the transaction,com2 is execute and com1 not save in db

Community
  • 1
  • 1
Mary
  • 581
  • 4
  • 8
  • 16
  • 1
    You need to enlist the distributed transaction coordinator – undefined Feb 11 '13 at 07:18
  • What currently happens? it looks very much like this should already work. However, you should remove `TT` from that completely: you are already handling the transaction via `TransactionScope`. So: what happens with the code in the recently edited version? Remove `TT` / `tt` completely. – Marc Gravell Feb 11 '13 at 08:03
  • when remove tt but not work codes. – Mary Feb 11 '13 at 08:08
  • when is com2 not execute,i want rooleback com1,but not work,already com1 insert into ConAcnt – Mary Feb 11 '13 at 08:10

1 Answers1

1

You probably need Distributed Transactions

dutzu
  • 3,883
  • 13
  • 19