Transaction and abnormal processing in ASP.NET
Author: Wawe Time: 2002-9-28 Recommended Level: ★ View author information and author anthology
-------------------------------------------------- ------------------------------ Reprinted from: DOTNET Chinese Technology Network Using SQL-Transaction Class and .NET provides exception handling mechanism We can handle problems in the database operation in a reliable manner and discovery system abnormality. This small article will explain the concept and usage of transaction processing and exception handling. What is a transaction? Transaction processing is a series of operations completed by a single logical unit, which can consist of a series of SQL statements, select, insert, update, delete, if there is no error after the operation is completed, then it The change in the database is permanent. If there is an error, it will not modify or change the database. To define a transaction, you need to use the Begin TRAN command, any statement after this command will be considered part of the transaction. Command Commital is used to complete the transaction and make the transaction to the changes to the database permanently. Use the ROLLBACK command to cancel a transaction and restore the transaction to the changes to the database. Here is an example of a business: [SQL Server7.0 or SQL Server2000] Begin TRAN INSERT INTO Product (ProductID, ProductName) Values ("0001", "Keyboard") Update Product Set Price = 12 Where ProductId = "0002" IF @Error> 0) Rollback else commit What is an abnormality? Develop an error message processing mechanism and provide useful, clear, meaningful information is one of the tasks of programmers, and exception handling is a mechanism that provides this service. Once the transaction fails, the server will issue a database error message to the system to help users discover and repair the State Union. We can handle such exception information in an abnormality and fix the faults. Usage of exception handling functions As shown below: [C #] try {// Database operation command} Catch (Exception E) {// If there is an abnormality, this part of the statement will be executed} Finally {? // Whether there is abnormality What happens, this part of the statement will be implemented} How to implement transactions? 1. Write a transaction statement during a stored procedure and use the following control to find whether or not there is an error, return the corresponding value, the Internet application displays the correct and easy-to-understand error message according to the returned value.
The following is an example of a transaction: [Store Procedure] CREATE PROCEDURE PRODUCT_SAVE (ASDECLARE (@USERID CHAR (5), @ LOCATION VARCHAR (50), @ RETURNS INT OUTPUT) BEGIN TRANUPDATE ADDRESS SET LOCATION = @ LOCATION WHERE USERID = @ USERIDIF ( @@ Error> 0) Begin @ returns = -1 / * fail to update * / rollbackendelse @ Returns = 0 / * succeed to update * / commitreturn @returns [Web Application In C #] int value; dbclass dbc = new dbclass () ; // Generate a database class VALUES = dbc.Updatedb ("0001", "23 rain street"); // and call @ 2xt = "Update SuccessFully" "Elselable_Message.Text =" Sorry, CAN Not Update this Record, please contact your DBA. "The above example is ideal for DBA, which is very familiar with database programming, and they prefer to complete exception handling functions during stored procedures. if Not familiar with database programming, then the following methods can be taken: 2. In the .NET framework, we can use the SQLTransaction class to define a transaction. So, we can use the commit or rollback function to control the transaction. Of course, we can also Acquire system exception using the anomaly handling function provided by the .NET framework.