using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace TransactionExample { class Program { static void Main(string[] args) { try { SqlConnection conn = new SqlConnection(); conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks;User Id=sa;Password=v044728V;"; conn.Open(); SqlTransaction tr = conn.BeginTransaction(); SqlCommand cmd = conn.CreateCommand(); cmd.Transaction = tr; try { cmd.CommandText = "UPDATE Person.Contact SET FirstName = 'Kim' WHERE ContactId = 8"; cmd.ExecuteNonQuery(); cmd.CommandText = "UPDATE Person.Contact SET FirstName = 'Kim' WHERE ContactId = 15"; cmd.ExecuteNonQuery(); tr.Commit(); Console.WriteLine("Names successfully changed."); } catch (Exception ex) { Console.WriteLine(ex.Message); tr.Rollback(); } finally { if (conn.State == System.Data.ConnectionState.Open) conn.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } } }