Another asp transaction processing method

Source: Internet
Author: User
 
<%
'Asp transaction processing.
'The test database is SQL server, the server is the local machine, the database name is test, the table name is a, two field IDs (int) primary key identifiers, num (int)
Set conn = server. CreateObject ("adodb. connection ")
StrConn = "provider = sqloledb.1; persist security info = false; uid = sa; pwd = sa; Initial Catalog = test; Data Source = ."
Conn. Open strConn
'The above code establishes a database connection
Conn. BeginTrans 'start transaction
StrSql1 = "update a set num = 1000 where id = 24" 'The first SQL statement is update. (Correct syntax)
StrSql2 = "insert into a (num) values ('A')" 'The second SQL statement is an incorrect SQL statement
StrSql3 = "insert into a (num) values (33333)" 'Third SQL statement is the correct SQL statement
Call conn.exe cute (strSql1)
Call conn.exe cute (strSql2)
Call conn.exe cute (strSql3)
If conn. Errors. Count = 0 then
Conn. CommitTrans 'if there is no conn error, the transaction is committed.
Else
Conn. RollbackTrans 'or roll back
End if
%>
After debugging, the above code can be processed normally. However, sometimes we do not want to display compilation errors to users.
Then we need to add On error resume next After conn. BeginTrans.
However, On error resume next is used. Conn. Errors. Count can only obtain the results returned by the conn of the last database operation. The preceding three SQL statements are invalid because the last SQL statement is correct. Then we need to make corresponding changes to the error handling.
If conn. Errors. Count = 0 then should be changed to if err. number = 0 then
In this way, we can perform other corresponding operations or prompts after database rollback. The modified code is as follows:
<%
Set conn = server. CreateObject ("adodb. connection ")
StrConn = "provider = sqloledb.1; persist security info = false; uid = sa; pwd = sa; Initial Catalog = test; Data Source = ."
Conn. Open strConn
'The above code establishes a database connection
Conn. BeginTrans 'start transaction
On error resume next 'added code
StrSql1 = "update a set num = 1000 where id = 24" 'The first SQL statement is update. (Correct syntax)
StrSql2 = "insert into a (num) values ('A')" 'The second SQL statement is an incorrect SQL statement

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.