Search Tools Links Login

ADO Transactions


Transactions are atomic operations that allow you to do multiple operations on a database as one operation. For example, if you were creating a banking application in which you deducted $100 from one account and added it to another account, you wouldn't want the operation to fail right in the middle, because the money would be 'lost'! The solution is to wrap the SQL in a transaction. If the operation is aborted in the middle (the pc gets shut off for example) the database will rollback the changes so that the initial account was never debited the $100. This will make you feel good, especially if its your bank account!

Original Author: Found on the World Wide Web

Code

<% Response.Expires = 0 %>

<H3>Transactions</H3>

<%
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open Application("guestDSN")
Set rs = Server.CreateObject("ADODB.RecordSet")
MySQL = "SELECT * FROM paulen"
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.ActiveConnection = cn
rs.Source = MySQL
rs.Open
%>

Before:



<% For i = 0 to RS.Fields.Count - 1 %>

<% Next %>

<% Do While Not RS.EOF %>

<% For i = 0 to RS.Fields.Count - 1 %>

<% Next %>

<%
RS.MoveNext
Loop
RS.Close
%>
<% = RS(i).Name %>
<% = RS(i) %>

<%
cn.BeginTrans
cn.Execute("INSERT INTO paulen (fld1, fld2) VALUES ('Aborted', 50)")
cn.RollbackTrans
cn.BeginTrans
cn.Execute("INSERT INTO paulen (fld1, fld2) VALUES ('Trans" & Time() & "', 100)")
cn.CommitTrans
%>
Completed.


After:




<%
rs.Open
For i = 0 to RS.Fields.Count - 1 %>

<% Next %>

<% Do While Not RS.EOF %>

<% For i = 0 to RS.Fields.Count - 1 %>

<% Next %>

<%
RS.MoveNext
Loop
RS.Close
Cn.Close
%>
<% = RS(i).Name %>
<% = RS(i) %>


About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 90 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.