

#include<array.au3>


#region - Allgemeine Deklaration
Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Initialisieren COM error handler
#endregion


#region - Database Deklaration (im Bsp. Firebird)

; Verbindungsstring:
Global $strConnection = "DRIVER={PostgreSQL UNICODE}; SERVER=127.0.0.1; UID=postgres; PWD=1234; DBNAME=postgres;"


; SQL-String zum Auslesen Feld NAME1 für einen Kunden aus Tabelle Kunden
Global $sql_str = "Select t1 from test;"
Global $array
#endregion - Database


#region - Beispielsprogramm
; DB-Verbindungsobjekt wird erstellt:
$oConn = _oSQLDB_Connect($strConnection)
If @error Then Exit MsgBox(0, 'Datenbank', 'keine Verbindung')


; Ausführung eines SQL-Statements mit Methode Execute ==> liefert Recordset zurück:
$oRecSet = _oSQLDB_Execute($oConn, $sql_str)
If @error Then
MsgBox(0, 'Execute', 'Fehler Beim Ausführen des SQL-Statements')
_close()
EndIf


; Recordset in Array wandeln:
$array = $oRecSet.GetRows
If Not IsArray($array) Then
MsgBox(0, 'Array', 'kein Ergebnis')
_close()
EndIf
_ArrayDisplay($array)


_close()


Func _close()
If IsObj($oRecSet) Then _oRecSet_Close($oRecSet)
If IsObj($oConn) Then _oSQLDB_Close($oConn)
Exit
EndFunc
#endregion - Beispielsprogramm


#region - Functions
;===============================================================================
; Function Name: _oSQLDB_Connect($connStr)
; Description:: erstellt eine Objektreferenz auf die im $connStr angegebene Datenbank
; Parameter(s): $connStr Connection-String enthält die Verbindungsparameter
; Return Value(s): Erfolg Referenz des ADODB-Objektes
; Fehler 1, @error 1
;===============================================================================
Func _oSQLDB_Connect($connStr)
Local $objConnection = ObjCreate("ADODB.Connection")
If Not IsObj($objConnection) Then Return SetError(1,0,1)
$objConnection.Open($connStr)
Return $objConnection
EndFunc ;==>_oSQLDB_Connect


;===============================================================================
; Function Name: _oSQLDB_Execute($oConn, $strCommand, $Options=-1)
; Description:: führt Kommandostring (SQL-Sequenz) aus
; Parameter(s): $oConn Referenz des Connection-Objektes
; $strCommand Kommandostring
; optional $Options CommandTypeEnum (http://msdn.microsoft.com/en-us/library/ms675946(VS.85).aspx) oder
; ExecuteOptionEnum (http://msdn.microsoft.com/en-us/library/ms676517(VS.85).aspx)
; Return Value(s): Eine Recordset Objekt-Referenz, ein Stream oder Nichts
;===============================================================================
Func _oSQLDB_Execute($oConn, $strCommand, $Options=-1)
Local $recAffected
Return $oConn.Execute($strCommand, $recAffected, $Options)
EndFunc ;==>_oSQLDB_Execute


;===============================================================================
; Function Name: _oRecSet_Close($oRecSet)
; Description:: Beendet ein RecordsetObjekt
; Parameter(s): $oRecSet Referenz des Objektes
;===============================================================================
Func _oRecSet_Close($oRecSet)
$oRecSet.Close
EndFunc ;==>_oRecSet_Close


;===============================================================================
; Function Name: _oSQLDB_Close($oConn)
; Description:: Beendet die Verbindung
; Parameter(s): $oConn Referenz des Connection-Objektes
;===============================================================================
Func _oSQLDB_Close($oConn)
$oConn.Close
EndFunc ;==>_oSQLDB_Close


Func MyErrFunc()
Msgbox(0,"AutoItCOM Test","Ein COM-Fehler ist aufgetreten !" & @CRLF & @CRLF & _
"err.description is: " & @TAB & $oMyError.description & @CRLF & _
"err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
"err.number is: " & @TAB & hex($oMyError.number,8) & @CRLF & _
"err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
"err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
"err.source is: " & @TAB & $oMyError.source & @CRLF & _
"err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
"err.helpcontext is: " & @TAB & $oMyError.helpcontext )
Local $err = $oMyError.number
If $err = 0 Then $err = -1
Local $g_eventerror = $err
Endfunc
#endregion - Functions
