【问题标题】:Run-Time error '-2147217871 (80040e31)': Query timeout expired运行时错误“-2147217871 (80040e31)”:查询超时已过期
【发布时间】:2015-12-13 06:00:44
【问题描述】:

当我尝试运行这个大查询时会出现这个错误。较小的查询不是。我已将连接超时设置为 0,但这无济于事。看起来连接成功了,它在代码的 .Open 部分挂断了。感谢您的帮助。

Public Sub dbConnectTDaY()
    Dim cn As ADODB.Connection
    Dim Rec_set As ADODB.Recordset
    Dim MyConn, varSQL As String
    Dim Rw As Long, Col As Long, c As Long
    Dim MyField, Location As Range
    Dim i As Integer

    Dim strSQL As String
    Dim strStDt As String
    Dim strEnDt As String
    strStDt = ThisWorkbook.Worksheets("xxx").Range("B6").Value
    strEnDt = ThisWorkbook.Worksheets("xxx").Range("B5").Value
    Set cn = New ADODB.Connection
    Set Rec_set = New ADODB.Recordset
    Set Location = [A2]
        Rw = Location.Row
        Col = Location.Column
        c = Col

    strSQL = ""
    strSQL = strSQL & "SELECT gp.cnt"
    strSQL = strSQL & ",gp.pod"
    strSQL = strSQL & ",gp.grp_paddsa"
    strSQL = strSQL & ",gp.grp_rasdd"
   '  etc.........



    cn.ConnectionTimeout = 0 'To wait till the query finishes without generating error

    cn.Open "DSN=#EDWP;Databasename=INTY;Uid=XXXXX;Pwd=XXXXX;"


    If cn.State = adStateOpen Then 'If connection is success, continue 'Check for errors...

        Rec_set.Open strSQL, cn 'Issue SQL statement
        For i = 0 To Rec_set.Fields.Count - 1
                ActiveSheet.Cells(1, i + 1) = Rec_set.Fields(i).Name
        Next i
        Do Until Rec_set.EOF
            For Each MyField In Rec_set.Fields
                Cells(Rw, c) = MyField
                c = c + 1
            Next MyField
            Rec_set.MoveNext
            Rw = Rw + 1
            c = Col
        Loop
    End If
     Rec_set.Close
    cn.Close

End Sub

【问题讨论】:

    标签: excel odbc teradata


    【解决方案1】:

    cn.ConnectionTimeout = 0 只影响连接超时。

    您还想设置CommandTimeout

    【讨论】:

    • 谢谢。这回答了它
    • 我在输入命令超时后仍然出现错误。有人可以帮忙吗?
    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多