Nested Implicit Transaction : SqlTransaction « ADO.Net « C# / CSharp Tutorial






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.");
            }
        }

    }








32.28.SqlTransaction
32.28.1.Updating Data Using Transactions
32.28.2.the use of a transaction
32.28.3.Nested Implicit Transaction
32.28.4.Implicit Transaction
32.28.5.Explicit Transaction