使用数据读取器获取Timeout到期错误

时间:2015-02-17 08:28:29

标签: sql sql-server vb.net

使用VB.Net和SQL

代码

 myConnection = New SqlConnection(Connection)
        myConnection.Open()
        Dim myCommand As SqlCommand = New SqlCommand("proc_g_report", myConnection)
        Dim myReader As SqlDataReader = myCommand.ExecuteReader() 

        DTResults.Load(myReader)

投掷错误为“超时已过期。操作完成前经过的超时时间或服务器未响应” 此行Dim myReader As SqlDataReader = myCommand.ExecuteReader()

出错

由于安全原因,我无法更改数据库池大小,请任何人建议。

2 个答案:

答案 0 :(得分:1)

尝试设置CommandTimeout

        myConnection = New SqlConnection(Connection)
        myConnection.Open()
        Dim myCommand As SqlCommand = New SqlCommand("proc_g_report", myConnection)
        myCommand.CommandTimeout=0
        Dim myReader As SqlDataReader = myCommand.ExecuteReader() 

        DTResults.Load(myReader)

答案 1 :(得分:1)

错误似乎无关,但是,如果要调用存储过程的执行,则需要告知传递的CommandType是什么。

默认值为CommandType = CommandType.Text,因此您的命令文本(proc_g_report)被视为SELECT / INSERT或其他标准T-SQL语句。

您需要设置CommandType并可能在一次性对象周围添加Using Statement ....

Using myConnection = New SqlConnection(Connection)
Using myCommand = New SqlCommand("proc_g_report", myConnection)
    myCommand.CommandType = CommandType.StoredProcedure
    myConnection.Open()
    Using myReader = myCommand.ExecuteReader() 
         DTResults.Load(myReader)
    End Using
End Using
End Using

当然,您可以使用Sql Server Management Studio轻松测试这是否是问题,并从您工作的PC调用该存储过程并检查完成所需的时间。如果时间少于30秒(SqlCommand执行的默认超时),则问题出在其他地方,而不是Timeout值。

相关问题