The simplest and most concise method
1. In the database tutorial, the main use of the Begin Transaction,commit Transaction,rollback transaction Three operations to implement, Additionally, an error occurred during the execution of the SQL statement by using the @ @error function.
-->
declare @error int
BEGIN TRANSACTION
Update Accountofbank Set balance = balance + 5000 where AccountName = ' John '
set @error = @error + @@ Error
Update Accountofbank set balance = balance - 5000 where accountname = ' dick '
set @error = @error + @ @error
if (@error <> 0 )
Rollback TRANSACTION
else
Commit TRANSACTION
2. Use the ASP tutorial. NET transaction classes to manipulate
-->
SqlConnection con= NewSqlConnection ("server=.; database=test;uid=sa;pwd=123456");
Con.open ();
SqlTransaction Tran=con.begintransaction ();
SqlCommand cmd= NewSqlCommand ();
Cmd.connection=con;
Cmd.transaction=Tran;
Try
{
Cmd.commandtext= "Update Accountofbank set balance=balance-5000 where Accountname= ' John '";
Cmd.executenonquery ();
Cmd.commandtext= "Update Accountofbank set balance=balance+5000 where accountname= ' Dick '";
Cmd.executenonquery ();
Tran.commit ();
}
Catch(Exception)
{
Tran.rollback ();
}
finally
{
Con.close ();
}
To
sum up
Transaction control or transaction management refers to the ability of a relational database management system to perform database transactions. A transaction is the most basic unit of work in which the SQL statements in a transaction must be performed in a logical order, and the operation of the entire unit of work is performed successfully or is not performed at all.
For example, there is a table area, save the user's home address, at the same time, by two users simultaneously to a table to operate, a user's operation is:
SELECT * FROM area
The other one is:
Update area (address) VALUES ("Shenzhen West Road, Futian District, Shenzhen")
Then the first user selected the address is the original data in the bitterness of the address, or the updated address? The results show that the original information is invalid when a change occurs during the select process.
In SQL Server, the statement that creates the transaction is:
Begin {Transaction | tran}[transaction_name]
Since our book is not specifically about SQL Server, there is not much to be done here, and we are interested in
transactionoptions options = new Transactionoptions ();
Options.isolationlevel = system.transactions.isolationlevel.readcommitted;
options.timeout = transactionmanager.defaulttimeout;
using (TransactionScope scope = new TransactionScope (transactionscopeoption.required, options))
{
//content
}