如何在Sql Server 2008中将ConnectionTimeout设置为0?

问题描述:

我将超时设置为0,但连接已提前关闭,此语句出了什么问题?

I set timeout to 0 but the connection close prematuraly, what is wrong with this statement ?

        Using odbcconn As New OdbcConnection(DataShared.gstrCNN)

        odbcconn.ConnectionTimeout = 0
        odbcconn.Open()
        Dim OdbcCmd As New OdbcCommand( _
            "{ ? = CALL [proc_Cp_GenEstadoCta](" & _
            PCOD_EMPR & ", " & _
            PPER_ANUAL & "," & _
            DataShared.gintCODUSER & " ) }", odbcconn)

        OdbcCmd.Parameters.Add("@return", OdbcType.Int)
        OdbcCmd.Parameters("@return").Direction = ParameterDirection.ReturnValue

        OdbcCmd.ExecuteNonQuery()
        If CInt(OdbcCmd.Parameters("@return").Value) = 0 Then
            GenEstadoMovsSaldos = True
        Else
            GenEstadoMovsSaldos = False
        End If

    End Using

更正的代码

        Using odbcconn As New OdbcConnection(DataShared.gstrCNN)

        --odbcconn.ConnectionTimeout = 0

        odbcconn.Open()
        Dim OdbcCmd As New OdbcCommand( _
            "{ ? = CALL [proc_Cp_GenEstadoCta](" & _
            PCOD_EMPR & ", " & _
            PPER_ANUAL & "," & _
            DataShared.gintCODUSER & " ) }", odbcconn)

        OdbcCmd.CommandTimeout = 60

        OdbcCmd.Parameters.Add("@return", OdbcType.Int)
        OdbcCmd.Parameters("@return").Direction = ParameterDirection.ReturnValue

        OdbcCmd.ExecuteNonQuery()
        If CInt(OdbcCmd.Parameters("@return").Value) = 0 Then
            GenEstadoMovsSaldos = True
        Else
            GenEstadoMovsSaldos = False
        End If

    End Using

运行良好!

通过将连接超时设置为零,您要完成什么工作?

What are you trying to accomplish by setting the connection timeout to zero?

连接超时是尝试打开数据库连接时要等待的时间.与连接何时关闭无关.

The connection timeout is the time to wait while attempting to open the database connection. It has nothing to do with when the connection closes.

也许您正在OdbcCommand类中寻找CommandTimeout属性?将CommandTimeout设置为零将删除等待查询运行的时间限制.

Perhaps you are looking for the CommandTimeout property in the OdbcCommand class? Setting the CommandTimeout to zero will remove the time limit when waiting for the query to run.

但是,如果数据库脱机,则程序将无限期地等待不会发生的事情,因此您应该考虑设置较长的时间,这样命令最终将超时而不是从不超时.

However, if the database goes offline your program will wait indefinitely for something that will not happen, so you should consider setting a long time instead, so that the command will timeout eventually instead of never.

(顺便说一句,为什么要使用ODBC数据库驱动程序而不是更快,更专业的SqlClient驱动程序?)

(By the way, why are you using the ODBC database driver instead of the faster and more specialised SqlClient driver?)