iT邦幫忙

0

Autoit連SqlSever

請問已經可以正確連結資料庫並出現成功訊息,但是假設給他錯誤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 ;

ccutmis iT邦高手 2 級 ‧ 2018-03-02 11:02:10 檢舉
https://stackoverflow.com/questions/7415093/need-to-execute-sql-query-in-autoit
chs965033 iT邦新手 5 級 ‧ 2018-03-02 11:42:19 檢舉
謝謝大大,我是用此範例測試,只是說為什麼
if @error Then
MsgBox(0, "ERROR", "Failed to connect to the database")
Exit
這段無法有效執行
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
haoming
iT邦好手 1 級 ‧ 2018-03-02 10:05:16

會不會是你的sql server 認證方式 不適認證 sql server id, 而是認證 windows id。
所以你用 autoit 連線時後,其實不是送出windows credential?

chs965033 iT邦新手 5 級 ‧ 2018-03-02 11:45:26 檢舉

謝謝大大,我確認過是使用sql server id,應該說autoit已經連線上資料庫了,只是說我在模擬假設server ip跑掉 或者設定有誤他應該要跑error視窗但他沒有出現,網路上範例大家也是用這樣方法寫得,不知是不是哪個環節有問題導致連線成功會有視窗提示,但是失敗卻沒有提醒

0
dahuh229
iT邦見習生 ‧ 2019-06-01 01:51:25

應該是連不到相關的物件早成的錯誤,要用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

我要發表回答

立即登入回答