【问题标题】:VBA Excel - SQL "execute" method triggered query timeout expired [duplicate]VBA Excel - SQL“执行”方法触发查询超时过期[重复]
【发布时间】:2021-03-15 11:06:00
【问题描述】:

这与Excel VBA: ODBC SQL Server driver query timeout expired中的问题不同

以下代码行触发:

[Microsoft] [ODBC SQL Server Driver] Query timeout expired` after 30 seconds.

代码:

Dim conn As ADODB.Connection 
Dim rs As ADODB.Recordset

Set rs = conn.Execute(WOString)

我的问题是,如何延长 Execute 方法的超时时间? Application.ODBCTimeout = 100conn.CommandTimeout = 100 无效。

如果它有任何重要性,我使用标准连接字符串(但连接没有任何问题):

Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

【问题讨论】:

  • 假设您使用的是 ADO,可以通过连接 CommandTimeout 属性指定查询超时(以秒为单位)。例如,conn.CommandTimeout = 300。在命令对象的情况下,设置命令的属性,因为它不是从连接继承的。
  • 我试过了,但是不行
  • 然而,有趣的是,这正是 OP 说这不是 @DanGuzman 的重复问题的答案,所以我 假设 OP 已经尝试过那行不通?也许那没有使用 ADO?
  • 如果它不起作用,我的 ADO 假设就是不正确的。问题中缺少的一个重要细节是 conn 实际上是什么对象类型。为清楚起见,添加 DIM conn 语句。
  • 那听起来不对,@BOB,如果在您首选的 IDE 中运行查询时查询花费的时间不到一秒,如果在 VBA 中不应该超过 30 秒;听起来我们在这里遗漏了一些东西。

标签: sql-server excel vba


【解决方案1】:

CommandTimeout 工作正常。见再现:

---------test.vbs------------

Sub testSub()
    Dim cn 'As Object
    Dim cnStr 'As String
    cnStr = "DRIVER=SQL Server;SERVER=localhost;Database=master;Trusted_Connection=true"
    Set cn = CreateObject("ADODB.Connection")
    With cn
        .ConnectionString = cnStr
        .ConnectionTimeout = 10
        .CommandTimeout = 100
        .Open
        If .State = 1 Then
            .Execute("waitfor delay '00:01:00'")
        End If
    End With
End Sub

testSub()

然后运行

 cscript .\test.vbs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多