asp.net the way of dealing with things

Source: Internet
Author: User

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
}
 

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.