Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Nested Implicit Transaction

using System; using System.Data; using System.Data.SqlClient; using System.Transactions; using System.Collections.Generic; using System.Text;     class Program     {         static SqlConnection conn;         static void Main(string[] args)         {             try             {                 using (TransactionScope scope = new TransactionScope())                 {                     using (conn = new SqlConnection("data source=localhost; initial catalog=SampleDB; Integrated Security=SSPI;"))                     {                         conn.Open();                         for (int x = 1; x < 8; x++)                         {                             using (TransactionScope scope1 = new TransactionScope())                             {                                 SqlCommand cmd = conn.CreateCommand();                                 cmd.CommandText = "DELETE Employees WHERE ID = " + x.ToString();                                 cmd.ExecuteNonQuery();                                 if (x < 3)                                     scope1.Complete();                             }                         }                     }                     scope.Complete();                 }             }             catch (TransactionAbortedException )             {                 Console.WriteLine("One or more of the child scopes voted to abort the transaction.");             }         }     }