String SQL1 = "...";
String SQL2 = "...";
SqlConnection Conn = New SqlConnection ("..."); // Establish a database connection
Conn.open (); // Receive the database connection
SQLTransaction Tran = conn.begintransaction (); // Start transaction processing
Sqlcommand comm = new sqlcommand ();
Comm.Connection = conn;
Comm.Transaction = Tran;
Try
{
Comm.commandtext = SQL1;
Comm.executenonQuery ();
Comm.commandtext = SQL2;
Comm.executenonQuery ();
Tran.commit (); // Accept trading, complete operation
} catch
{
Tran.rollback (); // Transaction failed, recovery data
}
Finally
{
Conn.close (); // Turn off the database connection.
}
2. Database method
Begin Transaction
Select * from student
SELECT * FORM CODE
IF (@@ Error> 0)
Rollback Transaction
Else
Commit transaction