How to set ConnectionTimeout to 0 in Sql Server 2008?

余生长醉 提交于 2019-12-08 11:06:38

问题


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

The corrected code

        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

It's working well !


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/2008309/how-to-set-connectiontimeout-to-0-in-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!