【发布时间】:2018-02-15 21:09:35
【问题描述】:
运行一段时间后,以下查询显示“超时已过期错误”。我什至设置了“cmd.CommandTimeout = 3600”,但是运行 1 分钟后我得到了“超时过期错误”
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim cmd As New ADODB.Command
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=server1;" & _
"Initial Catalog=database1;" & _
"Integrated Security=SSPI;"
cmd.ActiveConnection = conn
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
cmd.CommandTimeout = 3600
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("select column1, column2 from table1;")
' Check we have data.
If Not rs.EOF Then
' Transfer result.
Sheets(1).Range("A8").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
End Sub
请告诉我需要更改代码以使其长时间运行并显示结果。
【问题讨论】:
-
超时 = 0 会发生什么?
-
不知你是否需要设置连接超时而不是命令。
-
它是 Connection.CommandTimeout = 0
-
不是在连接上设置的,而是在命令
cmd.CommandTimeout = 3600上设置的。此外,命令超时的默认值为 30,因此如果它在超时前 60 秒(不确定您是否真的计时),那么有些东西是关闭的。