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)