ADODB查询超时

20

我试图打开一个查询,但它超时了。我尝试设置了timeout属性,但似乎不想接受它。

使用MS-SQL Server Management窗口(SQL Server 2005)执行该查询需要34秒,因此我知道需要增加超时时间。

当前代码:

Public Function retRecordSet(StrSQL)
Dim cmd ' as new ADODB.Command
Dim rs 'As New ADODB.Recordset

Set cmd = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")

cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandText = StrSQL
cmd.CommandTimeout = 0
Set rs = cmd.Execute

Set retRecordSet = rs
End Function

我还尝试设置连接本身的超时时间 CurrentProject.Connection.CommandTimeout = 120,但是如果我在执行此命令后立即查询该值,则其值仍为30

连接属性:

Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=False;Data Source=MyServer;Integrated Security=SSPI;Initial Catalog=MyDatabase;Data Provider=SQLOLEDB.1

Data Source Object Threading Model = 1
Multiple Results = 3
Multiple Parameter Sets = False
SQL Support = 283
Catalog Location = 1
Catalog Term = database
Catalog Usage = 15
Rowset Conversions on Command = True
Extended Properties = 
Cache Authentication = True
Encrypt Password = 
Persist Encrypted = 
Persist Security Info = False
Asynchronous Processing = 0
Connect Timeout = 600
Protection Level = 
Prompt = 4
Mode = 
Location = 
Locale Identifier = 1033
Impersonation Level = 
Window Handle = 
Data Source = MyServer
User ID = 
Password = 
Integrated Security = SSPI
Mask Password = 
Initial Catalog = MyDatabase
Lock Owner = 
Bind Flags = 
General Timeout = 0
Data Provider = SQLOLEDB.1
Autocommit Isolation Levels = 4096
Unique Reshape Names = False

3
请查看此博客文章:http://codingjourney.blogspot.com/2008/11/ado-connection-timeout-command-or.html - HK1
1
@HK1,你会看到我将ADODB命令的命令超时设置为0(无限超时)。 - SeanC
不确定为什么30秒的超时时间仍然存在,这似乎非常奇怪。如果您调用“rs.open cmd”而不是将命令对象作为源传递给“set rs = cmd.execute”,会发生什么?只是好奇是否会遇到同样的问题? - Fink
你的连接设置怎么样?当我使用ADO连接时,我总是使用我自己创建的显式连接,所以我不习惯使用CurrentProject.Connection。这是一个ADP吗? - HK1
@HK1,是的,这是一个ADP(我无法更改)。 - SeanC
@SeanCheshire 只是出于兴趣 - CommandTimeout = 0 是否意味着不设置超时? - whytheq
3个回答

33

不确定您是否已经解决了问题,但我遇到过同样的问题。 我是通过Recordset.Open SQL_String, Connection来完成的。

在此之前,我只设置了超时属性,不是在Recordset或Command上,而是在连接对象上:

Connection.CommandTimeout = 0

16

来自http://codingjourney.blogspot.com/2008/11/ado-connection-timeout-command-or.html的内容:

ADO连接超时:命令或操作正在进行,无法执行该操作。

这个错误通常是由于查询需要太长时间而导致的。默认情况下,ADO Command对象的CommandTimeout属性设置为30秒。如果查询需要更长时间才能完成,则会发生此错误。

解决方法:

  • 增加CommandTimeout属性的值,以增加等待时间。
  • 优化查询以减少查询所需的时间。

The Solution

You must also set the commandTimeout property on the ADODB.Command or ADODB.Recordset being used. Otherwise those objects will use the default time limit of 30 seconds because they do not inherit the time limit from the associated ADODB.Connection instance.

Example Using VBScript in ASP 3:

set con = createObject("ADODB.Connection")
con.open connectionString
con.commandTimeout = 60
set command = createObject("ADODB.Command")
command.activeConnection = con
command.commandType = adCmdText
command.commandText = sql
command.commandTimeout = 60
command.execute
response.write command.commandTimeout 'This is now 60 seconds.

关于此事:“您还必须在使用的ADODB.Command或ADODB.Recordset上设置commandTimeout属性。”我没有看到ADODB.Recordset对象上的CommandTimeout属性或方法。 - Syntax Junkie

1
对于OLEDB,您不需要在连接上指定超时时间:

Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=False;Data Source=MyServer;Integrated Security=SSPI;Initial Catalog=MyDatabase;Data Provider=SQLOLEDB.1;Connect Timeout=30


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接