請問已經可以正確連結資料庫並出現成功訊息,但是假設給他錯誤Server ip或者password他不跳出ERROR視窗而是會直接當掉(因為連不到正確的資料庫)?有甚麼方式可以判斷資料錯誤,而直接顯示連結失敗的提醒視窗?
$constrim="DRIVER={SQL Server};SERVER=XXXX;DATABASE=XX;uid=XX;pwd=XX;"
$adCN = ObjCreate ("ADODB.Connection") ;
$adCN.Open ($constrim)
if @error Then
MsgBox(0, "ERROR", "Failed to connect to the database")
Exit
Else
MsgBox(0, "Success!", "Connection to database successful!)
EndIf
$adCN.Close ;
會不會是你的sql server 認證方式 不適認證 sql server id, 而是認證 windows id。
所以你用 autoit 連線時後,其實不是送出windows credential?
應該是連不到相關的物件早成的錯誤,要用on error去處理
可參考下列這邊文章內容
https://www.autoitscript.com/forum/topic/84341-on-error-resume-next/
Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling.
$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
$oIE.bogus
if $g_eventerror then Msgbox(0,"","the previous line got an error.")
Exit
; This is my custom error handler
Func MyErrFunc()
$HexNumber=hex($oMyError.number,8)
Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
"Number is: " & $HexNumber & @CRLF & _
"Windescription is: " & $oMyError.windescription )
$g_eventerror = 1 ; something to check for when this function returns
Endfunc