Transaction Processing during ASP. NET development

Source: Internet
Author: User

Today, I re-read chapter 7 of "asp.net advanced programming design version 4". I didn't care much about asp.net's transaction processing, however, it is really appropriate to say that: Let's know new things.

Today, there are not many content, and there are not many code demos, so you can take a break early.

Transaction: a transaction can be understood as an operation that must be completed at the same time or fail at all. Classic example in the book: account transfer, A account and B account must be A-100 at the same time B + 100, this operation must be successful at the same time, as long as there is A failure two steps need to roll back, such a complete process can be called a [transaction ].

 

The entire section describes the transaction processing as follows:

  • Stored Procedures implement transactions: This is a good way to implement transactions.

Syntax: BEGIN TRANSACTION

Successful: COMMIT

Failed: ROLLBACK

The following is a simple example to demonstrate the usage:

Create procedure myTransaction (@ amount MONEY, --- transfer amount @ countA INT, -- account A @ countB INT -- account B) as begin transaction --- here is your custom SQL update statement A/**/-- @ ERROR, which is always A variable that records the ERROR information of the currently executed SQL statement, after each update, the system automatically sets 0 IF (@ ERROR> 0) ROLLBACK again. Here is your custom SQL update statement B/**/IF (@ ERROR> 0) ROLLBACK --- all updates have been executed COMMIT -- Update Transaction Status RETURN
  • ADO. NET implements transactions

Syntax: start by calling the BeginTransaction () method of the connection object. This method returns a Transaction object for managing transactions. Because ADO. NET provides different ADO for different database providers.. NET object. The corresponding transaction object class names are also different, for example, SqlTransaction OledbTransaction OracleTransaction. Here we take the SQL Server data provider as an example to provide a standard example, mainly to demonstrate usage:

Successful: con. Commit ();

Failed: con. Rollback ();

SqlConnection con = new SqlConnection (strConnect); SqlCommand cmd1 = new SqlCommand (strSQL1, con); SqlCommand cmd2 = new SqlCommand (strSQL2, con); SqlTransaction tran = null; try {con. open (); tran = con. beginTransaction (); // create a transaction // submit the Cmd object to the transaction. Statement 1.transaction = tran; statement 2.transaction = tran; // execute the update statement 1.executenonquery (); statement 2.executenonquery (); // all updates are successful. Refresh the transaction status tran. commit ();} catch {// update failed transaction rollback tran. rollback ();} finally {con. close ();}

 

  • COM + implements transactions: But there is no detail in the book to discuss this part.

 

 

Related Article

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.