1. transactions in a normal program:
Using (system. Transactions. transactionscope Ts = new transactionscope ())
{
Try
{
// SQL statement
TS. Complete (); // submit the transaction
}
Catch (exception E)
{
Logger. writelog (E. Message );
}
Finally
{
TS. Dispose (); // release the transaction
}
}
2. Transactions in SQL statements:
Public void Shiwu (){
Try {
Sqlconnection conn = new sqlconnection (sqlhelper. connectionstring );
Conn. open ();
Sqltransaction TRAN = NULL;
TRAN = conn. begintransaction ();
Sqlcommand cmd = new sqlcommand ();
Cmd. Transaction = Tran; // suspend the transaction
Cmd. Connection = conn;
SQL _insert = SQL; // SQL statement
Dbhelpersql. preparecommand (CMD, Conn, Tran, SQL _insert, null );
Int val = cmd. executenonquery ();
Tran. Commit (); // transaction committed successfully
}
Catch (exception E)
{
Tran. rollback (); // transaction rollback if an error is reported
}
Finally
{
Conn. Close (); // close the connection
}
}