conn.connectiontimeout,conn.commandtimeout,command.commandtimeout?

设置超时时有几个地方需要注意:
一、Connection的超时,包括Connection.ConnectionTimeout和Connection.CommandTimeout两部分,都必须设好;

二、Command的超时,需要设Command.CommandTimeout的值。

如果直接用Connection对象来Execute一个SQL语句,那它的CommandTimeout必须设好;
如果用一个Command对象来执行SQL语句,那Command对象的Commandtimeout必须设好;
当然,无论哪种情况,Connection对象的ConnectionTimeout是必须要设的。
ConnectionTimeOut As Integer 默认 15秒
例子:

' DSNLess Connection using the OLE DB provider for ODBC - MSDASQL
Public Function ConnectSQLServer(ByVal ServerName As String, ByVal DBName As String, ByVal UserName As String, ByVal PWD As String, Optional ByVal ConnectionTimeOut As Integer = 15) As Boolean
    Dim strConnect As String
   
    On Error GoTo Error_ConnectSQLServer
    ' DSNLess Connection using the OLE DB provider for ODBC - MSDASQL
    cn.CursorLocation = adUseClient
    strConnect = "Driver={SQL Server};Server=" & ServerName & ";DataBase=" & DBName & ";UID=" & UserName & ";PWD=" & PWD & ";"
    cn.ConnectionTimeOut = ConnectionTimeOut
    cn.Open strConnect
       
    ConnectSQLServer = True
    Exit Function
   
Error_ConnectSQLServer:
    ConnectSQLServer = False
End Function

ConnectionTimeout 属性
     

指示在终止尝试和产生错误前建立连接期间所等待的时间。

设置和返回值

设置或返回指示等待连接打开的时间的长整型值(单位为秒)。默认值为 15。

说明

如果由于网络拥塞或服务器负载过重导致的延迟使得必须放弃连接尝试时,请使用 Connection 对象的 ConnectionTimeout 属性。如果打开连接前所经过的时间超过 ConnectionTimeout 属性上设置的时间,将产生错误,并且 ADO 将取消该尝试。如果将该属性设置为零,ADO 将无限等待直到连接打开。请确认正在对其编写代码的提供者会支持 ConnectionTimeout 功能。

连接关闭时 ConnectionTimeout 属性为读/写,而打开时其属性为只读。

CommandTimeout 属性
     

指示在终止尝试和产生错误之前执行命令期间需等待的时间。

设置和返回值

设置或返回长整型值,该值指示等待命令执行的时间(单位为秒)。默认值为 30。

说明

使用 Connection 对象或 Command 上的 CommandTimeout 属性,允许由于网络拥塞或服务器负载过重产生的延迟而取消 Execute 方法调用。如果在 CommandTimeout 属性中设置的时间间隔内没有完成命令执行,将产生错误,然后 ADO 将取消该命令。如果将该属性设置为零,ADO 将无限期等待直到命令执行完毕。请确保正在写入代码的提供者和数据源支持 CommandTimeout 功能。

Connection 对象的 CommandTimeout 设置不会对相同 Connection 上 Command 对象的 CommandTimeout 设置产生影响,即 Command 对象的 CommandTimeout 属性不继承 Connection 对象的 CommandTimeout 的值。

在 Connection 对象上,打开 Connection 后,CommandTimeout 属性将保持读/写。

posted @ 2008-04-10 10:51  Athrun  阅读(2803)  评论(0编辑  收藏  举报