Jump to content

_SQL.au3. ADODB.Connection


ChrisL
 Share

Recommended Posts

36 minutes ago, Andreik said:

MSDN says the default ConnectionTimeout is 15 seconds. I don't see a particular reason why it takes 30 seconds in your case but you never know with these DB providers. Some of them might not even implement ConnectionTimeout. What provider do you use? Let us see the connection string.

Sorry you're right, it takes 30 seconds only when both servers fail

Im using Sql server express 2019

you can see the connection string in my post above

 

Edited by Holy_Boy

Sorry if my grammar isn't proper, English is my third Language... 

Link to comment
Share on other sites

This should work. I tested the code with Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64)   Sep 24 2019 13:48:23   Copyright (C) 2019 Microsoft Corporation  Developer Edition (64-bit) on Windows 10 Home 10.0 <X64> (Build 22621: ).

#include <_sql.au3>

_SQL_RegisterErrorHandler()

; Create ADODB.Connection object
$oADODB = _SQL_Startup()
If @error Then
    MsgBox(0x10, 'Error', 'Failed to create ADODB.Connection object')
    Exit
EndIf

; Create an array to store connection credentials (2 servers in my case)
; This array can be obtained from an ini file like in your case
; [Server, Database, Username, Password]
Local $aServerInfo[2][4] = [['some-inexistent-server', 'master', 'sa', 'test'], ['localhost', 'master', 'sa', 'test']]

$Connect = Connect($oADODB, $aServerInfo, 2)
If @error Or ($Connect = $SQL_ERROR) Then
    MsgBox(0x10, 'Error', 'Could not estabilish a connection to any server.')
Else
    MsgBox(0x40, 'Yayyy', 'Connection estabilished with ' & $aServerInfo[@extended][0] & '.')
EndIf

_SQL_Close($oADODB)
_SQL_UnRegisterErrorHandler()

Func Connect($oADODB, $aServerInfo, $iTimeout = 15)
    ; Connect to first available server for a connections pool
    If Not IsObj($oADODB) Then Return SetError(1, 0, Null)
    If Not IsArray($aServerInfo) Then Return SetError(2, 0, Null)
    _SQL_ConnectionTimeout($oADODB, $iTimeout)
    For $Index = 0 To UBound($aServerInfo) - 1
        If _SQL_Connect($oADODB, $aServerInfo[$Index][0], $aServerInfo[$Index][1], $aServerInfo[$Index][2], $aServerInfo[$Index][3]) = $SQL_OK Then
            ; If we successfully connect to a server
            Return SetError(0, $Index, $SQL_OK)
        EndIf
    Next
    ; If we couldn't connect to any server
    Return SetError(0, 0, $SQL_ERROR)
EndFunc

I typed on purpose first server name to something that doesn't exist and then my second server name it's just localhost. After 2 seconds (since first connection fails on purpose) or so I managed to connect to localhost as expected. Give it a try.

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

On 7/31/2023 at 9:32 AM, mLipok said:

You use wrong logic here:

try this:

 ElseIf $server2=$SQL_ERROR Then

:)

Did you try this ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

8 hours ago, mLipok said:

Did you try this ?

yes, else if didnt work, because if the first condition is true it wont execute the else part at all. I just created another if else statement 

Sorry if my grammar isn't proper, English is my third Language... 

Link to comment
Share on other sites

I think that you do not understand what I refering to.
But also my logic was also wrong

Your code:

$server1=_SQL_Connect(-1,$server,$database,$username,$password)
;.......
if $server1=$SQL_ERROR Then
    ;.......
    $server2=_SQL_Connect(-1,$server,$database,$username,$password)
    ;.......
Else
    ;.......
    If $server2=$SQL_ERROR Then ; NEVER HAPPEND
        ;.......
        $server3=_SQL_Connect(-1,$server,$database,$username,$password)
        ;.......
    EndIf
    ;.......
EndFunc

 

my previous still not valid logic:

$server1=_SQL_Connect(-1,$server,$database,$username,$password)
;.......
if $server1=$SQL_ERROR Then
    ;.......
    $server2=_SQL_Connect(-1,$server,$database,$username,$password)
    ;.......
ElseIf $server2=$SQL_ERROR Then ; NEVER HAPPEND
        ;.......
        $server3=_SQL_Connect(-1,$server,$database,$username,$password)
        ;.......
    EndIf
    ;.......
EndFunc

 

my revised proposal:

$server1=_SQL_Connect(-1,$server,$database,$username,$password)
;.......
if $server1=$SQL_ERROR Then
    ;.......
    $server2=_SQL_Connect(-1,$server,$database,$username,$password)
    ;.......
;~ Else ; DO NOT USE "ELSE" HERE 
    ;.......
    If $server2=$SQL_ERROR Then
        ;.......
        $server3=_SQL_Connect(-1,$server,$database,$username,$password)
        ;.......
    EndIf
    ;.......
EndFunc

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

7 hours ago, mLipok said:

I think that you do not understand what I refering to.
But also my logic was also wrong

Your code:

$server1=_SQL_Connect(-1,$server,$database,$username,$password)
;.......
if $server1=$SQL_ERROR Then
    ;.......
    $server2=_SQL_Connect(-1,$server,$database,$username,$password)
    ;.......
Else
    ;.......
    If $server2=$SQL_ERROR Then ; NEVER HAPPEND
        ;.......
        $server3=_SQL_Connect(-1,$server,$database,$username,$password)
        ;.......
    EndIf
    ;.......
EndFunc

 

my previous still not valid logic:

$server1=_SQL_Connect(-1,$server,$database,$username,$password)
;.......
if $server1=$SQL_ERROR Then
    ;.......
    $server2=_SQL_Connect(-1,$server,$database,$username,$password)
    ;.......
ElseIf $server2=$SQL_ERROR Then ; NEVER HAPPEND
        ;.......
        $server3=_SQL_Connect(-1,$server,$database,$username,$password)
        ;.......
    EndIf
    ;.......
EndFunc

 

my revised proposal:

$server1=_SQL_Connect(-1,$server,$database,$username,$password)
;.......
if $server1=$SQL_ERROR Then
    ;.......
    $server2=_SQL_Connect(-1,$server,$database,$username,$password)
    ;.......
;~ Else ; DO NOT USE "ELSE" HERE 
    ;.......
    If $server2=$SQL_ERROR Then
        ;.......
        $server3=_SQL_Connect(-1,$server,$database,$username,$password)
        ;.......
    EndIf
    ;.......
EndFunc

 

It did very similar, I just used another endif before the second if

Sorry if my grammar isn't proper, English is my third Language... 

Link to comment
Share on other sites

13 hours ago, Andreik said:

Another wrong part is to set ConnectionTimeout after the attempt to connect to server. It should be set before and just one time since it's a persistent property.

Can you please post an example how the code should look like when trying to connect to a few servers like in my case?, I chanched the syntax like you said, but still takes very long each server to timeout 

Sorry if my grammar isn't proper, English is my third Language... 

Link to comment
Share on other sites

ConnectionTimeout should be set before a connection attempt is made. And you don't have to set it for next _SQL_Connect() calls if you don't want a different timeout.

_SQL_ConnectionTimeout(-1,2)
$server1=_SQL_Connect(-1,$server,$database,$username,$password)

 

When the words fail... music speaks.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...