【问题标题】:Timeout expired error in VBA?VBA中的超时过期错误?
【发布时间】: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 秒(不确定您是否真的计时),那么有些东西是关闭的。

标签: vba excel


【解决方案1】:

所以答案是两个部分。

如 cmets 中所述,将其设置在 Connection 对象上

为了有更长的时间,将此设置为 0。起初似乎有点反直觉。

Connection.CommandTimeout = 0 

我之前运行过的查询也遇到过这种情况。

这里也有讨论:

ADODB query timeout

【讨论】:

  • 如果您不介意,您可以编辑我的代码并告诉我如何编辑代码吗?只是为了更清晰的想法。
  • put conn.CommandTimeOut = 0 开启连接前设置,
猜你喜欢
  • 2012-06-01
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
相关资源
最近更新 更多