NET implicit transaction

Source: Internet
Author: User
To run distributed transactions, you need to start the T-SQL: CreateTableTranTable (IdINTIDENTITY (255) PRIMARYKEY, PriorityTINYINT -- maximum value 256) C #: stringsql1INSERTINTOTranTableVALUES (1); normal insertion limit ); insert exception ADO

To run distributed transactions, you need to start the T-SQL: CreateTableTranTable (IdINTIDENTITY (255) PRIMARYKEY, PriorityTINYINT -- maximum) C #: stringsql1 = "INSERTINTOTranTableVALUES (1 )"; // normal insertion of stringsql2 = "INSERTINTOTranTableVALUES (256)"; // an exception occurs during insertion of ADO.

To run distributed transactions, Hong Kong Space, and U.S. servers, you need to start

T-SQL:

Create Table TranTable

(

Id int identity (1, 1) primary key,

Priority TINYINT -- maximum value: 255

)

C #:

String sql1 = "insert into TranTable VALUES (1)"; // INSERT normally

String sql2 = "insert into TranTable VALUES (256)"; // INSERT an exception

ADO. NET transaction

Using (SqlConnection conn = new SqlConnection (ConfigurationManager. ConnectionStrings ["MySqlServer"]. ConnectionString ))
{
Conn. Open ();
Using (SqlTransaction tran = conn. BeginTransaction ())
{
Using (SqlCommand cmd = new SqlCommand ())
{
Cmd. Connection = conn;
Cmd. CommandType = CommandType. Text;
Cmd. Transaction = tran;
Try
{
Cmd. CommandText = sql1;
Cmd. ExecuteNonQuery ();
Cmd. CommandText = sql2;
Cmd. ExecuteNonQuery ();
Tran. Commit ();
Response. Write ("OK ");
}
Catch (SqlException ex)
{
Tran. Rollback ();
Response. Write ("Error:" + ex. Message );
}
}
}
Conn. Close ();
}

ADO. NET

TransactionScope)

Implicit transactions do not have

Using (SqlConnection conn = new SqlConnection (ConfigurationManager. ConnectionStrings ["MySqlServer"]. ConnectionString ))
{
Using (TransactionScope ts = new TransactionScope ())
{
Conn. Open ();
Using (SqlCommand cmd = new SqlCommand ())
{
Cmd. Connection = conn;
Cmd. CommandType = CommandType. Text;
Try
{
Cmd. CommandText = "insert into TranTable (Priority) values (1 )";
Cmd. ExecuteNonQuery ();
Cmd. CommandText = "insert into TranTable (Priority) values (256 )";
Cmd. ExecuteNonQuery ();
Ts. Complete ();
Response. Write ("OK ");
}
Catch (SqlException ex)
{
Response. Write ("Error:" + ex. Message );
}
}
}
Conn. Close ();
}

TransactionScope is not directly associated with the database. How does it implement statement execution in the way of transactions?

If we add

Distributed Transaction display (CommittableTransaction)

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.