设置ADODB查询超时

时间:2019-07-16 08:16:48

标签: sql sql-server excel vba timeout

我正试图强制获得超时过期错误消息。

我将Connection.CommandTimeOut设置为1,但是执行了20秒的查询却没有遇到任何麻烦。

Public Function getdata(query As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim connstring As String
Set cnn = New ADODB.Connection

connstring = "Provider=SQLOLEDB;Data Source=omitted;uid=omitted;pwd=omitted;database=omitted;Connect Timeout=180"
cnn.Open connstring
cnn.CommandTimeout = 1
Set getdata = New ADODB.Recordset
    getdata.CursorLocation = adUseClient
    Debug.Print cnn.CommandTimeout 'prints 1, as expected

getdata.Open query, connstring, adOpenStatic, adLockBatchOptimistic

cnn.Close
End Function

如何正确设置尝试执行查询的最大允许时间?

0 个答案:

没有答案