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.