简体   繁体   English

关闭IDataReader时执行超时已过期

[英]Execution Timeout Expired when closing IDataReader

I'm retrieving huge amount of data from SQL Server. 我正在从SQL Server检索大量数据。 Everything works fine but when I try to close IDataReader inside using statement 一切正常,但是当我尝试使用using语句关闭IDataReader时

try
    {
      using (SqlConnection con = new SqlConnection(connString))
       {
         con.Open();
         using (SqlCommand command = new SqlCommand(sql_query, con))
         {
           command.CommandTimeout = 0;

           using (IDataReader rdr = new SqlCommand(sql_query, con).ExecuteReader(CommandBehavior.SequentialAccess))
            {
              dataTable = GetDataTableFromDataReader(rdr);
            }
             ....

I'm getting: Execution Timeout Expired. 我得到:执行超时已过期。 The timeout period elapsed prior to completion of the operation or the server is not responding. 在操作完成之前超时或服务器没有响应。

Would it be better to use something like rdr = null ? 使用rdr = null这样更好吗? Or is there another better solution how to close IDataReader? 还是有另一个更好的解决方案如何关闭IDataReader?

Note that there are timeout settings on both the connection and the command. 请注意,连接和命令上都有超时设置。 You don't need to close or dispose manually inside a using block. 您无需在using块内手动关闭或处置。

You are creating a second SqlCommand in the using block which does not have a timeout set. 您正在using块中创建第二个SqlCommand ,该命令没有设置超时。 Change to: 改成:

using (IDataReader rdr = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
    dataTable = GetDataTableFromDataReader(rdr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM