Distributed transaction Processing TransactionScope

Source: Internet
Author: User

For. NET developer, distributed transactions can be reduced to the ability to perform transactions on more than 2 SqlConnection connection objects, because no matter where the database is placed, the connection string is only different for the developer, Different connection strings need to correspond to different SqlConnection connection objects

The TransactionScope class under the System.Transaction namespace can handle distributed transactions

Before using the System.Transaction class, add the System.Transaction.dll

The using is equivalent to the scope after use. Dispose (); String Sql=string.empty; using (TransactionScope scope=new transactionscope) {string connstring=@ "Data source=hp-sql server/sql2005db;initial Catalog=northwind;user Id=sa; password=123456 "; Using (SqlConnection conn=new SqlConnection (connstring)) {conn.open (); try {sql= "insert into Regin (Reginid, Regindescript) VALUES (' 5 ', ' Testregin '); SqlCommand cmd=new SqlCommand (); Cmd. Connection=conn; Cmd.commandtext=sql; Cmd. ExecuteNonQuery (); catch (Exception ex) {Response.Write (ex.message);}} Scope.complete (); When the complete () method is executed at the end of the transaction, TransactionScope automatically commits the transaction}

Note: Distributed transaction execution is not managed by Ado.net, but is managed by the MSDTC service, a COM + based technology

The Distributed transaction management supported by the TransactionScope class is not done by the TransactionScope class, but rather by the DTC service that is submitted to the local machine, so that before using distributed transactions, the DTC service of the native is turned on, running net Start The MSDTC command can

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.