asp.net sql事务

 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);               

}           

}       

}

posted @ 2011-05-18 10:19  枫-  阅读(296)  评论(0编辑  收藏  举报