Jump to content
Sign in to follow this  

SQL Error Handling

Recommended Posts


Is it possible to perform some type of error handling with SQL? I currently use a function that writes a line to SQL but if the server is down or even worse the server is up but not working correctly, how can I prevent the script from just completely bombing out. I primarily use the SQL for logging and if that fails the rest of the script still needs to run. Here is a sample of the function I am using...

Func logtosql($exittext)
    $DBCon = ObjCreate ("ADODB.Connection")
    $DBCon.Open ("Provider=SQLOLEDB;Data Source=SERVER;Initial Catalog=DBNAME;User Id=USERNAME;Password=PASSWORD")
    $DBCon.Execute ("INSERT INTO unlockexitlog (remotemachine,datetimestamp,exittext) Values ('"&$remotePC&"','"&@MON&"/"&@MDAY&"/"&@YEAR&" "&@HOUR&":"&@MIN&":"&@SEC&"','"&$exittext&"')")

Share this post

Link to post
Share on other sites

I'm no expert on this, but in the beta help file it shows how to set up a custom error handler for dealing with COM errors. This might help you trap various errors and branch appropriately. Do a search for "COM Error Handling"

BlueBearrOddly enough, this is what I do for fun.

Share this post

Link to post
Share on other sites

Thank you, I found what you are talking about and plan to look though the examples and see what I can come up with.

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Install a custom error handler 

; Performing a deliberate failure here (object does not exist)
$oIE = ObjCreate("InternetExplorer.Application")
$oIE.visible = 1
if @error then Msgbox(0,"","the previous line got an error.")


; This is my custom error handler 
Func MyErrFunc() 
   Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription ) 

   SetError(1) ; something to check for when this function returns 

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.