【问题标题】:Getting Timeout expire error using data reader使用 datareader 获取超时过期错误
【发布时间】:2015-02-17 08:28:28
【问题描述】:

使用 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()

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

【问题讨论】:

  • 你试过了吗myCommand.CommandTimeout=0
  • @Ganesh,我必须在哪一行之后使用
  • 发布答案试试
  • 您是否考虑过优化存储过程?
  • 不确定您是否已经发布了所有代码,但是如果您想调用 StoredProcedure 您需要将 CommandType 设置为 CommandType.StoredProcedure 否则 db 无法理解您的命令文本

标签: sql sql-server vb.net


【解决方案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)

【讨论】:

    【解决方案2】:

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

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

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

    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 值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-01
      • 2011-08-02
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 2021-12-11
      • 2020-11-28
      • 2021-12-31
      相关资源
      最近更新 更多