Transaction

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Transactions;
namespace TransactionTest
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         ConnectionStringSettings cnSetting =
            ConfigurationManager.ConnectionStrings["NorthwindString"];
         using (SqlConnection cn = new SqlConnection())
         {
            cn.ConnectionString = cnSetting.ConnectionString;
            cn.Open();
            using (SqlTransaction tran = cn.BeginTransaction())
            {
               try
               {
                  //work code here
                  using (SqlCommand cmd = cn.CreateCommand())
                  {
                     cmd.Transaction = tran;
                     cmd.CommandText = "Select count(*) from employees";
                     int count = (int)cmd.ExecuteScalar();
                     MessageBox.Show(count.ToString());
                  }
                  //if we made it this far, commit
                  tran.Commit();
               }
               catch (Exception xcp)
               {
                  tran.Rollback();
                  //cleanup code
                  MessageBox.Show(xcp.Message);
               }
            }
         }
      }
       
      private void button2_Click(object sender, EventArgs e)
      {
         ConnectionStringSettings cnSetting =
            ConfigurationManager.ConnectionStrings["NorthwindString"];
         using (SqlConnection cn = new SqlConnection())
         {
            cn.ConnectionString = cnSetting.ConnectionString;
            cn.Open();
            using (SqlTransaction tran =
               cn.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
               try
               {
                  //work code here
                  using (SqlCommand cmd = cn.CreateCommand())
                  {
                     cmd.Transaction = tran;
                     cmd.CommandText = "Select count(*) from employees";
                     int count = (int)cmd.ExecuteScalar();
                     MessageBox.Show(count.ToString());
                  }
                  //if we made it this far, commit
                  tran.Commit();
               }
               catch (Exception xcp)
               {
                  tran.Rollback();
                  //cleanup code
                  MessageBox.Show(xcp.Message);
               }
            }
         }
      }
       private void button3_Click(object sender, EventArgs e)
       {
           ConnectionStringSettings cnSetting =
              ConfigurationManager.ConnectionStrings["NorthwindString"];
           using (TransactionScope ts = new TransactionScope())
           {
               using (SqlConnection cn = new SqlConnection())
               {
                   cn.ConnectionString = cnSetting.ConnectionString;
                   cn.Open();
                   //work code here
                   using (SqlCommand cmd = cn.CreateCommand())
                   {
                       cmd.CommandText = "Select count(*) from employees";
                       int count = (int)cmd.ExecuteScalar();
                       MessageBox.Show(count.ToString());
                   }
                   //if we made it this far, commit
                   ts.Complete();
               }
           }
       }
      private void button4_Click(object sender, EventArgs e)
      {
         ConnectionStringSettings cnSetting =
            ConfigurationManager.ConnectionStrings["NorthwindString"];
         TransactionOptions opt = new TransactionOptions();
         opt.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
         using (TransactionScope ts =
            new TransactionScope(TransactionScopeOption.Required, opt))
         {
            using (SqlConnection cn = new SqlConnection())
            {
               cn.ConnectionString = cnSetting.ConnectionString;
               cn.Open();
               //work code here
               using (SqlCommand cmd = cn.CreateCommand())
               {
                  cmd.CommandText = "Select count(*) from employees";
                  int count = (int)cmd.ExecuteScalar();
                  MessageBox.Show(count.ToString());
               }
            }
            //if we made it this far, commit
            ts.Complete();
         }
      }
      private void button5_Click(object sender, EventArgs e)
      {
         ConnectionStringSettings cnSetting =
            ConfigurationManager.ConnectionStrings["NorthwindString"];
         TransactionOptions opt = new TransactionOptions();
         opt.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
         using (TransactionScope ts =
            new TransactionScope(TransactionScopeOption.Required, opt))
         {
            using (SqlConnection cn = new SqlConnection())
            {
               cn.ConnectionString = cnSetting.ConnectionString;
               cn.Open();
               //work code here
               using (SqlCommand cmd = cn.CreateCommand())
               {
                  cmd.CommandText = "Select count(*) from employees";
                  int count = (int)cmd.ExecuteScalar();
                  MessageBox.Show(count.ToString());
               }
            }
            using (SqlConnection cn = new SqlConnection())
            {
               cn.ConnectionString = cnSetting.ConnectionString;
               cn.Open();
               //work code here
               using (SqlCommand cmd = cn.CreateCommand())
               {
                  cmd.CommandText = "Select count(*) from employees";
                  int count = (int)cmd.ExecuteScalar();
                  MessageBox.Show(count.ToString());
               }
            }
            //if we made it this far, commit
            ts.Complete();
         }
      }
      private void button6_Click(object sender, EventArgs e)
      {
         Employee e1 = new Employee(1, "JoeLast", "JoeFirst");
         Employee e2 = new Employee(2, "MaryLast", "MaryFirst");
         try
         {
            using (TransactionScope scope = new TransactionScope())
            {
               e1.LastName = "JoeTranLast";
               e2.LastName = "MaryTranLast";
               scope.Complete();
            }
         }
         catch (Exception xcp)
         {
            System.Diagnostics.Debug.WriteLine("Exception: " + xcp.Message);
         }
         System.Diagnostics.Debug.WriteLine(
            "Final Answer:\r\n" + e1.ToString());
         System.Diagnostics.Debug.WriteLine(
            "Final Answer:\r\n" + e2.ToString());
      }
   }
}

你可能感兴趣的:(职场,休闲)