19

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

使用 MS-SQL Server 管理窗口 (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
4

3 回答 3

31

不确定您是否已经解决了这个问题,但我遇到了同样的问题。我正在使用 Recordset.Open SQL_String,Connection 来做这件事。

在此之前,我只是设置了 timeout 属性,而不是在 Recordset 或 Command 上,而是在 Connection 对象上:

Connection.CommandTimeout = 0
于 2014-04-30T07:30:52.987 回答
15

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

解决方案

您还必须设置或正在使用的commandTimeout属性 。否则,这些对象将使用 30 秒的默认时间限制,因为它们不会从关联实例继承时间限制。ADODB.CommandADODB.RecordsetADODB.Connection

在 ASP 3 中使用 VBScript 的示例:

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.
于 2018-06-28T07:19:09.793 回答
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

于 2013-07-11T08:20:35.783 回答