0
嘗試使用下面的代碼測試連接,該連接正常工作。但是,遇到無效/錯誤的連接字符串時,需要很長時間才能返回無法連接的錯誤消息。嘗試連接到遠程服務器時尤其如此。VBA測試Sql連接字符串是否正確(超時)
是否有超時設置或我可以設置讓它停止嘗試連接並更快地返回錯誤消息?
Private Sub btnTest_Click()
On Error GoTo ErrorHandler
Set cn = New ADODB.Connection
Set cm = New ADODB.Command
cs = "Provider=SQLNCLI11;Server=MyServer;Database=MyDatabase;Trusted_Connection=Yes;"
cn.Open cs
MsgBox "Connection successful.", vbInformation, "Test Mode Message"
cn.Close
Set cm = Nothing
Set cn = Nothing
ExitSub:
Exit Sub
ErrorHandler:
MsgBox "Cannot connect」
Resume ExitSub
End Sub
Thanks..That工作 – ptownbro