尝试运行一个简短的过程时查询超时过期

这是我的连接string:

Global Const strConn As String = _ "PROVIDER=SQLNCLI10;" & _ "P-SSWORD=blahblah;" & _ "USER ID=blahblah;" & _ "INITIAL CATALOG=blah;" & _ "DATA SOURCE=blah;" & _ "CONNECT TIMEOUT=0;" & _ "COMMAND TIMEMOUT=0" & _ "PACKET SIZE=4096;" 

这里是简单的代码:

 Sub MoveDataUsingADO() Dim cn As Object Dim cm As Object Dim rs As Object 'get in touch with the server 'Create ado objects. Set cn = CreateObject("ADODB.Connection") cn.connectiontimeout = 0 cn.Open strConn cn.CommandTimeout = 0 Set cm = CreateObject("ADODB.Command") Set cm.ActiveConnection = cn cm.CommandType = 4 'adCmdStoredProc Set rs = CreateObject("ADODB.Recordset") Set rs.ActiveConnection = cn cm.CommandText = "WH.dbo.ourProcName" With wb.Sheets("Data") .Activate .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 2).End(Excel.xlUp).Row + 1, .Cells(1, .Columns.Count).End(Excel.xlToLeft).Column)).ClearContents End With With rs .Open Source:=cm.Execute '<=====errors here=========== wb.Sheets("Data").Cells(wb.Sheets("Data").Rows.Count, 1).End(Excel.xlUp)(2, 1).CopyFromRecordset rs .Close 'close connection End With ... ... 

在上面标记的点,我得到以下错误:

在这里输入图像描述

我不明白 – 程序需要55秒运行,并在我的vba我已经设置超时为0 …我认为这迫使他们不过期?

你有没有试过在命令上明确设置超时时间?

 cm.CommandTimeout = 0 

而不是将其设置在连接上。 我认为连接超时处理的是它允许您在连接超时之前尝试build立连接的时间,而命令超时是指令需要执行的时间。

你有一些想法应该花多长时间来执行你的查询? 可能会有一个不同于超时时间的问题?

您应该尝试在命令对象上设置超时,如下所示:

 cm.CommandText = "WH.dbo.ourProcName" cm.CommandTimeout = 120 With wb.Sheets("Data") .Activate .Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 2).End(Excel.xlUp).Row + 1, .Cells(1, .Columns.Count).End(Excel.xlToLeft).Column)).ClearContents End With With rs .Open Source:=cm.Execute '<=====errors here=========== wb.Sheets("Data").Cells(wb.Sheets("Data").Rows.Count, 1).End(Excel.xlUp)(2, 1).CopyFromRecordset rs .Close 'close connection End With