Guest User

Untitled

a guest
Jul 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 4.02 KB | None | 0 0
  1. #include-once
  2.  
  3.  
  4. ;===============================================================================
  5. ;
  6. ; Function Name:    _SQLConnect
  7. ; Description:      Initiate a connection to a SQL database
  8. ; Syntax:           $oConn = _SQLConnect($sServer, $sDatabase, $fAuthMode = 0, $sUsername = "", $sPassword = "", _
  9. ;                       $sDriver = "{SQL Server}")
  10. ; Parameter(s):     $sServer - The server your database is on
  11. ;                   $sDatabase - Database to connect to
  12. ;                   $fAuthMode - Authorization mode (0 = Windows Logon, 1 = SQL) (default = 0)
  13. ;                   $sUsername - The username to connect to the database with (default = "")
  14. ;                   $sPassword - The password to connect to the database with (default = "")
  15. ;                   $sDriver (optional) the ODBC driver to use (default = "{SQL Server}")
  16. ; Requirement(s):   Autoit 3 with COM support
  17. ; Return Value(s):  On success - returns the connection object for subsequent SQL calls
  18. ;                   On failure - returns 0 and sets @error:
  19. ;                       @error=1 - Error opening database connection
  20. ;                       @error=2 - ODBC driver not installed
  21. ;                       @error=3 - ODBC connection failed
  22. ; Author(s):        SEO and unknown
  23. ; Note(s):          None
  24. ;
  25. ;===============================================================================
  26. Func _SQLConnect($sServer, $sDatabase, $fAuthMode = 0, $sUsername = "", $sPassword = "", $sDriver = "{SQL Server}")
  27.     Local $sTemp = StringMid($sDriver, 2, StringLen($sDriver) - 2)
  28.     Local $sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers", $sVal = RegRead($sKey, $sTemp)
  29.     If @error or $sVal = "" Then Return SetError(2, 0, 0)
  30.     $oConn = ObjCreate("ADODB.Connection")
  31.     If NOT IsObj($oConn) Then Return SetError(3, 0, 0)
  32.     If $fAuthMode Then $oConn.Open ("DRIVER=" & $sDriver & ";SERVER=" & $sServer & ";DATABASE=" & $sDatabase & ";UID=" & $sUsername & ";PWD=" & $sPassword & ";")
  33.     If NOT $fAuthMode Then $oConn.Open("DRIVER=" & $sDriver & ";SERVER=" & $sServer & ";DATABASE=" & $sDatabase)
  34.     If @error Then Return SetError(1, 0, 0)
  35.     Return $oConn
  36. EndFunc   ;==>_SQLConnect
  37.  
  38. ;===============================================================================
  39. ;
  40. ; Function Name:    _SQLConnect
  41. ; Description:      Send a query to a SQL database and return the results as an object
  42. ; Syntax:           $oQuery = _SQLQuery($oConn, $sQuery)
  43. ; Parameter(s):     $oConn - A database connection object created by a previous call to _SQLConnect
  44. ;                   $sQuery - The SQL query string to be executed by the SQL server
  45. ; Requirement(s):   Autoit 3 with COM support
  46. ; Return Value(s):  On success - returns the query result as an object
  47. ;                   On failure - returns 0 and sets @error:
  48. ;                       @error=1 - Unable to process the query
  49. ; Author(s):        SEO and unknown
  50. ; Note(s):          None
  51. ;
  52. ;===============================================================================
  53. Func _SQLQuery($oConn, $sQuery)
  54.     If IsObj($oConn) Then Return $oConn.Execute($sQuery)
  55.     Return SetError(1, 0, 0)
  56. EndFunc ;==>_SQLQuery
  57.  
  58. ;===============================================================================
  59. ;
  60. ; Function Name:    _SQLDisconnect
  61. ; Description:      Disconnect and close an existing connection to a SQL database
  62. ; Syntax:           _SQLDisconnect($oConn)
  63. ; Parameter(s):     $oConn - A database connection object created by a previous call to _SQLConnect
  64. ; Requirement(s):   Autoit 3 with COM support
  65. ; Return Value(s):  On success - returns 1 and closes the ODBC connection
  66. ;                   On failure - returns 0 and sets @error:
  67. ;                       @error=1 - Database connection object doesn't exist
  68. ; Author(s):        SEO and unknown
  69. ; Note(s):          None
  70. ;
  71. ;===============================================================================
  72. Func _SQLDisconnect($oConn)
  73.     If NOT IsObj($oConn) Then Return SetError(1, 0, 0)
  74.     $oConn.Close
  75.     Return 1
  76. EndFunc   ;==>_SQLDisconnect
Add Comment
Please, Sign In to add comment