.net 事务总结

1、ado.net

本例为更新数据库中某张表的一个示例。
.net 事务总结_第1张图片

2、COM+事务(分布式事务)

 .net 事务总结_第2张图片  .net 事务总结_第3张图片  .net 事务总结_第4张图片

3、 实例public void RunSqlTransaction(string myConnString) { (SqlConnection myConnection = new SqlConnection(myConnString); myConnection.Open(); SqlCommand myCommand = myConnection.CreateCommand(); SqlTransaction myTrans; // Start a local transaction myTrans = myConnection.BeginTransaction(); // Must assign both transaction object and connection // to Command object for a pending local transaction myCommand.Connection = myConnection; myCommand.Transaction = myTrans; myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, ´Description´)"; myCommand.ExecuteNonQuery(); myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, ´Description´)"; myCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine("Both records are written to database."); } catch(Exception e) { try { myTrans.Rollback(); } catch (SqlException ex) { if (myTrans.Connection != null) { Console.WriteLine("An exception of type " + ex.GetType() + " was encountered while attempting to roll back the transaction."); } } Console.WriteLine("An exception of type " + e.GetType() + " was encountered while inserting the data."); Console.WriteLine("Neither record was written to database."); } finally { myConnection.Close(); }

你可能感兴趣的:(.net 事务总结)