C#輕量級企業事務 - TransactionScope

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   art   

 1 using System; 2 using System.Data.SqlClient; 3 using System.Transactions; 4  5 namespace SomeDBTransaction 6 { 7     class Program 8     { 9         static void Main(string[] args)10         {11             string con1 = "SERVER=.; DATABASE=db1; UID=sa; PWD=llh";12             string con2 = "SERVER=.; DATABASE=db2; UID=sa; PWD=llh";13             string sqlStr1 = "U_t1";14             string sqlStr2 = "U_t1";15 16             int resu=CreateTransactionScope(con1, con2, sqlStr1, sqlStr2);17             Console.WriteLine("受影響的行數:"+resu);18 19             Console.Read();20         }21 22         // This function takes arguments for 2 connection strings and commands to create a transaction  23         // involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the  24         // transaction is rolled back. To test this code, you can connect to two different databases  25         // on the same server by altering the connection string, or to another 3rd party RDBMS by  26         // altering the code in the connection2 code block. 27         static public int CreateTransactionScope(28             string connectString1, string connectString2,29             string commandText1, string commandText2)30         {31             // Initialize the return value to zero and create a StringWriter to display results. 32             int returnValue = 0;33             System.IO.StringWriter writer = new System.IO.StringWriter();34 35             try36             {37                 // Create the TransactionScope to execute the commands, guaranteeing 38                 // that both commands can commit or roll back as a single unit of work. 39                 using (TransactionScope scope = new TransactionScope())40                 {41                     using (SqlConnection connection1 = new SqlConnection(connectString1))42                     {43                         // Opening the connection automatically enlists it in the  44                         // TransactionScope as a lightweight transaction.45                         connection1.Open();46 47                         // Create the SqlCommand object and execute the first command.48                         SqlCommand command1 = new SqlCommand(commandText1, connection1);49                         command1.CommandType = System.Data.CommandType.StoredProcedure;50                         returnValue = command1.ExecuteNonQuery();51                         writer.WriteLine("Rows to be affected by command1: {0}", returnValue);52 53                         // If you get here, this means that command1 succeeded. By nesting 54                         // the using block for connection2 inside that of connection1, you 55                         // conserve server and network resources as connection2 is opened 56                         // only when there is a chance that the transaction can commit.    57                         using (SqlConnection connection2 = new SqlConnection(connectString2))58                         {59                             // The transaction is escalated to a full distributed 60                             // transaction when connection2 is opened.61                             connection2.Open();62 63                             // Execute the second command in the second database.64                             returnValue = 0;65                             SqlCommand command2 = new SqlCommand(commandText2, connection2);66                             command1.CommandType = System.Data.CommandType.StoredProcedure;67                             returnValue = command2.ExecuteNonQuery();68                             writer.WriteLine("Rows to be affected by command2: {0}", returnValue);69                         }70                     }71 72                     // The Complete method commits the transaction. If an exception has been thrown, 73                     // Complete is not  called and the transaction is rolled back.74                     scope.Complete();75 76                 }77 78             }79             catch (TransactionAbortedException ex)80             {81                 writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);82             }83             catch (ApplicationException ex)84             {85                 writer.WriteLine("ApplicationException Message: {0}", ex.Message);86             }87             catch (Exception ex)88             {89                 writer.WriteLine("ERROR: {0}",ex.Message);90             }91             // Display messages.92             Console.WriteLine(writer.ToString());93 94             return returnValue;95         }96     }97 98 99 }
CODE

代碼很簡單啦 ~~,就多加了一個using而已

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.