C# 的事務編程
1 Db事務?
? DbConnection 中創建基于當前連接的 DbTransaction
?
2 ?使用TransactionScope ,創建環境事務
? 一旦創建,在這個環境包含的DbConnection 實例 都會根據連接字符串中的
Sqlserver 連接字符串支持,是否自動附加當前環境事務.
連接字符串關鍵字(Enlist)
?????? SqlConnection.ConnectionString 屬性支持關鍵字 Enlist,該關鍵字指示 System.Data.SqlClient 是否將檢測事務上下文并自動在分布式事務中登記連接。 如果 Enlist=true,連接將自動在打開的線程的當前事務上下文中登記。 如果 Enlist=false,SqlClient 連接不會與分布式事務進行交互。 Enlist 的默認值為 true。 如果連接字符串中未指定 Enlist,若在連接打開時檢測到一個,連接將自動在分布式事務中登記。??
Server=(local)SQL2005;Database=Northwind;Integrated Security=SSPI;auto-enlist=false
Mysql 等其他 OLDP數據庫 需要驅動支持。
?




以下來自MSDN:
System.Transactions?基礎結構提供了這兩個的顯式編程模型基于?Transaction?類,以及隱式編程模型使用?TransactionScope?類,在其中事務自動管理基礎結構。
![]() |
---|
建議您創建使用隱式事務?TransactionScope?類,以便為您自動管理環境事務上下文。?您還應該使用?TransactionScope?和?DependentTransaction?跨多個函數調用或多個線程調用需要使用相同的事務的應用程序的類。?此模型的詳細信息,請參閱?Implementing An Implicit Transaction Using Transaction Scope?主題。?編寫事務應用程序的詳細信息,請參閱?Writing A Transactional Application。 |
?
在實例化?TransactionScope?通過?new?語句中,事務管理器確定哪些事務參與進來。?一旦確定,該范圍將始終參與該事務。?此決策基于兩個因素:是否存在環境事務以及構造函數中?TransactionScopeOption?參數的值。?環境事務是在代碼中執行的事務。?可通過調用?Current?類的靜態?Transaction?屬性,獲取對環境事務的引用。?有關如何使用此參數的詳細信息,請參閱的"事務流的管理"部分?Implementing An Implicit Transaction Using Transaction Scope?主題。
如果在事務范圍內未不發生任何異常 (即之間的初始化?TransactionScope?對象并調用其?Dispose?方法),則范圍所參與的事務可以繼續。?如果在事務范圍內發生異常,參與到其中的事務將回滾。
當您的應用程序完成所有工作時它想要在事務中執行,應調用?Complete?方法一次,以通知該事務管理器是可接受,即可提交事務。?未能調用此方法中止事務。
調用?Dispose?方法將標記事務范圍的末尾。?在調用此方法之后所發生的異常不會影響事務。
如果您修改的值?Current?內某個范圍內,將引發異常時?Dispose?調用。?但是,在作用域結束時,以前的值被還原。?此外,如果您調用?Dispose?上?Current?在事務范圍創建事務,事務將中止范圍的末尾。
?
// This function takes arguments for 2 connection strings and commands to create a transaction // involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the // transaction is rolled back. To test this code, you can connect to two different databases // on the same server by altering the connection string, or to another 3rd party RDBMS by // altering the code in the connection2 code block. static public int CreateTransactionScope(string connectString1, string connectString2,string commandText1, string commandText2) {// Initialize the return value to zero and create a StringWriter to display results.int returnValue = 0;System.IO.StringWriter writer = new System.IO.StringWriter();try{// Create the TransactionScope to execute the commands, guaranteeing// that both commands can commit or roll back as a single unit of work.using (TransactionScope scope = new TransactionScope()){using (SqlConnection connection1 = new SqlConnection(connectString1)){// Opening the connection automatically enlists it in the // TransactionScope as a lightweight transaction. connection1.Open();// Create the SqlCommand object and execute the first command.SqlCommand command1 = new SqlCommand(commandText1, connection1);returnValue = command1.ExecuteNonQuery();writer.WriteLine("Rows to be affected by command1: {0}", returnValue);// If you get here, this means that command1 succeeded. By nesting// the using block for connection2 inside that of connection1, you// conserve server and network resources as connection2 is opened// only when there is a chance that the transaction can commit. using (SqlConnection connection2 = new SqlConnection(connectString2)){// The transaction is escalated to a full distributed// transaction when connection2 is opened. connection2.Open();// Execute the second command in the second database.returnValue = 0;SqlCommand command2 = new SqlCommand(commandText2, connection2);returnValue = command2.ExecuteNonQuery();writer.WriteLine("Rows to be affected by command2: {0}", returnValue);}}// The Complete method commits the transaction. If an exception has been thrown,// Complete is not called and the transaction is rolled back. scope.Complete();}}catch (TransactionAbortedException ex){writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);}catch (ApplicationException ex){writer.WriteLine("ApplicationException Message: {0}", ex.Message);}// Display messages. Console.WriteLine(writer.ToString());return returnValue; }
?