using (SqlConnection connection = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
transaction = connection.BeginTransaction("SampleTransaction");
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, ''Description'')"; command.ExecuteNonQuery();
command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, ''Description'')"; command.ExecuteNonQuery(); // Attempt to commit the transaction.
transaction.Commit();
}
catch (Exception ex)
{
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
//Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
//Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}