Friday, December 14, 2007

How to use the ADO Connection object to perform a Transaction?

How to use the ADO Connection object to perform a Transaction?

The Basic idea of the "Transaction" is that: if we have a lot of modifications of database, we can save all the modifications in a template. After we confirmed the transaction is completed, we can update all modifications into the database.

The advantage is that: when the transaction has not completed yet, the user can cancel all the modifications:

The ASP codes are written and described as below:


<%

On Error Resume Next 'When the ADO find the error, don’t s How it and perform next step

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

conn.BeginTrans 'Start Transaction

conn.execute "DELETE FORM User WHERE userid='admin'"

conn.execute "DELETE FORM User WHERE userid='admin1'"

if conn.Errors.Count = 0 then

conn.CommitTrans 'done, keep the changes

else

conn.RollBackTrans 'have something wrong, cancel the changes.

end if

conn.close

set conn = nothing

%>

No comments: