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