Recommended: ASP script loop statement This article teaches you the ASP script loop statement: The characteristic of the ASP dynamic server page environment is that it is written through one or several scripting languages. The scripting language can be regarded as a simplified version of the programming language. It is easy to learn and master, which provides considerable convenience to the designers of dynamic websites. It can be said that the proper use of scripting language is directly related to ASP
julyclyde(original)
The author is Microsoft China Community Star in February
In programming, transactions are often needed. The so-called transaction is a series of operations that must be successful. As long as one operation fails, all other steps must also be cancelled. For example, when developing a network hard disk system using ASP, the user registration part needs to do:
Record user information into the database
Open a folder for the user for storage
Initialize user operation log
Transactions must be used in these three steps, otherwise if the disk operation fails and the database operation is not revoked, it will cause dead users who can only log in but cannot operate.
Due to the special development history of the database system, everything from Access to DB2 is supported by transactions. Therefore, the above steps can be expressed as follows:
On Error Resume Next
first step:
Log user information into the database under a transaction environment
If Err Then
Close the connection
quit
Else
Step 2: Create a folder
If Err Then
Roll back the first step of database operation and exit
Else
Step 3: Operate the log database in a transaction environment
If Err Then
Roll back the first step and delete the folder created in the second step
quit
End If
End If
End If
Submit the first transaction of database operation
Submit transactions for the second step of database operation
End
Each step needs to be judged. If it fails, it is necessary to manually roll back the previous multiple steps, making the program complicated and difficult to understand. If you update the program in the future and add other steps, you will also need to nest more layers of If...Else...End If to make the program process more complicated.
The correct solution is to use the transaction control function of ASP. IIS can control multiple transaction-supporting systems through contact with MTS services. When the program sends a failed signal, all transaction-supporting systems will automatically roll back, even if the operation has been officially completed; it also provides a convenient manual rollback method for operations that do not support transactions. The above example is rewritten using the ASP transaction control function as follows:
<%@ TRANSACTION = Required %>
On Error Resume Next
Set Conn=Server.CreateObject(ADODB.Connection)
Conn.Open ......
Conn.Execute INSERT....
Conn.Close
Set Conn=Nothing
Set Conn2=Server.CreateObject(ADODB.Connection)
Conn2.Open...
Conn2.Execute INSERT....
Conn2.Close
Set Conn2=Nothing
Set FSO=Server.CreateObject(Scripting.FilessystemObject)
FSO.CreateFolder...
If Err Then
ObjectContext.SetAbort ' Notify all components that support transactions to rollback and run manual rollback code
Else
ObjectContext.SetComplete
End If
Set FSO=Nothing
Sub OnTransactionAbort
Response.Write Error
FSO.DeleteFile Server.Mappath(a.txt) 'FSO's manual rollback-delete folder
End Sub
Sub OnTransactionCommit
Response.Write Complete the mission successfully
End Sub
%>
The <%@ TRANSACTION = Required %> in the first line indicates that this page of the asp file requires MTS transaction support. Each operation in the middle is written in normal order without considering the rollback issue. At the end of the program, determine whether there is any error. If so, call the SetAbort method of ObjectContext, IIS will notify all transaction-supporting components to rollback (mainly database) through the MTS service, and run Sub OnTransactionAbort manually rollback operations that do not support transactions; if no error occurs, call the SetComplete method of ObjectContext, and run Sub OnTransactionCommit to display the successful message.
The entire ASP program does not need to write unnecessary code for judgment errors and rollback operations. It only needs to make judgments at the end. Even if multiple steps are added in the future, it only needs to be controlled in Sub OnTransactionAbort. It is very convenient. Programmers can focus on process writing instead of writing error correction code.
In fact, ASP also provides many more useful functions, waiting for us to use. Don’t think that ASP will definitely have weak functions when using scripting language.
Share: How to implement asp component-free thumbnail generation There are many ASP components that generate thumbnails on the Internet. If your virtual space does not support registering new components, you may feel that your website is losing its color. Xinqing is not talented, and combined with online resources, she wrote a component-free thumbnail program for reference only. Let's take a look at the basics first. First of all, we know that the following code is displayed on the page: img src=pic.gif border=0 width