[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I make a series of ADO sql statements atomic??

Posted on 2001-06-05
5
Medium Priority
?
175 Views
Last Modified: 2013-11-23
I have a series of 5 sql executes with my ADO object and I need them to either all execute or on error I need to rollback everyone that has previously executed.
0
Comment
Question by:mccainz2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Expert Comment

by:dbirdman
ID: 6157853
Use the ADO Connection object for Transaction Processing.
Prior to executing your sql statements, begin the transaction with:  cn.BeginTrans.  If one of the sql executes gets an error, use cn.RollbackTrans.  If they all complete successfully, use cn.CommitTrans.  I'm using this now in an app and it works great.

Good Luck
0
 
LVL 6

Accepted Solution

by:
dbirdman earned 200 total points
ID: 6157897
This may be a better examplen for the transaction.  The connection string is for SQL Server; just substitute it for the appropriate connection if you're not using SQL Server.  The Rollback will also handle any updates you may have done to a table within the Transaction.

Public Function Test() As Boolean

Dim cn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
Dim strConnection As String
Dim sql1 As String
Dim sql2 As String

Set cn = New ADODB.Connection
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset

cn.ConnectionString  = "Provider=SQLOLEDB;DataSource=MyServer;Initial Catalog=My Database; User ID=MyID; Password=MyPW"

cn.Open

sql1 = "Select * from Table1"
sql2 = "Select * from Table2"

'Begin Transaction
cn.BeginTrans

rs1.Open sql1, cn, adOpenDynamic, adLockPessimistic, adCmdText
  'Stop, for example, if no records are retrieved
If rs1.EOF Then
     cn.RollBackTrans
     Exit Function
End If

rs2.Open sql2, cn, adOpenDynamic, adLockPessimistic, adCmdText
  'Stop again if no records are retrieved
If rs2.EOF Then
     cn.RollBackTrans
     Exit Function
End If

'If no problems, commit the transaction
cn.CommitTrans

Test = True

End Function

Hope this helps

0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6158478
You can have a error handler, and in the error handler, you can place the rollback code, so that, if any error happens when executing one of the sql statements or whatever, then a rollback would occur.

The code could be something like this:

Dim lTrans As Boolean

On Error Go To ErrHandler
lTrans = False
:
:
Conn.Begintrans
lTrans = True
:
:
your sql statements here
:
:
Conn.CommitTrans
lTrans = False
:

ErrHandler:
If Err.Number <> 0 Then
   Msgbox "Could Not Proceed." & Err.Description & " :" & Err.Number
   If lTrans Then
     Conn.RollbackTrans
     lTrans = False
   End If
End If
0
 
LVL 1

Expert Comment

by:mukunth
ID: 6159124
As suggested in other comments u can use the transaction as follow:

cn.Begintrans
.
.
.
(execute the 5 SQL statements here by saying cn.Execute )
cn.execute "insert into test values(1)"
.
.
If cn.Errors.Count > 0 Then
cn.RollBackTrans
Msgbox "errors occured"
else
cn.CommitTrans
End If

where:
cn is the connection object
Hope this helps.
Cheers.
0
 
LVL 5

Author Comment

by:mccainz2
ID: 6160063
danka, for some idiotic reason I was assuming that on error it would behave by rolling back without me actually writing any error handling code....silly goose.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question