简体   繁体   English

循环时 ExecuteReader 执行超时已过期

[英]ExecuteReader execution timeout expired when looping

I am sorry before because the title too general.之前很抱歉,因为标题太笼统了。 I just wonder how it's happen in this code:我只是想知道它是如何在这段代码中发生的:

foreach (var item in list)
{
     .........
     using (SqlCommand cmd = new SqlCommand(@"SELECT some_fields FROM tbl WHERE id=@id", new SqlConnection(db.ConnectionString)))
     {
           cmd.Connection.Open();
           cmd.Parameters.AddWithValue("@id", item.id);

           var reader = cmd.ExecuteReader();
           while (reader.Read())
           {
                //do something
           }
           cmd.Connection.Close();
     }
     .........
}

Execution timeout happened on the second loop.执行超时发生在第二个循环中。 First loop was no problem.第一次循环没问题。 Is there something wrong in this syntax?这个语法有什么问题吗? Please tell me.请告诉我。

Exception thrown at ExecuteReader(): ExecuteReader() 抛出的异常:

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at HPM_WEB.Areas.InventoryManagementForms.Transaction.OutboundTransaction.InsertOrUpdateDetailOutbound(MOutboundAdviseModel mout, List`1 doutList, SqlConnection connection, SqlTransaction transaction, String message) in F:\MyFolder\..\Transaction.cs:line 444

Thanks in advance提前致谢

Try increasing the Connection Timeout尝试增加连接超时

// Setting command timeout to 3 Minutes (60*3=180 Seconds)
cmd.CommandTimeout = 60*3;

Do this before ExecuteReader()ExecuteReader()之前执行此操作

How long it takes for your cmd.ExecuteReader();你的cmd.ExecuteReader();需要多长时间cmd.ExecuteReader(); to execute?执行?

in your connectionstring, you may want to get a try by allowing multiple active resultset.在您的连接字符串中,您可能希望通过允许多个活动结果集来尝试一下。

see if this resolve your issue?看看这是否能解决您的问题? like:像:

<add name="yourDBConn" connectionString="Data Source=yourInstance;Initial Catalog=yourDB;Persist Security Info=True;User ID=user;Password=pwd;Connection Timeout=30;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />

A value of 0 indicates no limit (an attempt to execute a command will wait indefinitely).值 0 表示没有限制(尝试执行命令将无限期等待)。

The default is 30 seconds.默认值为 30 秒。 if Read requires two network packets, then it has 30 seconds to read both network packets.如果读取需要两个网络数据包,则有 30 秒的时间读取两个网络数据包。 If you call Read again, it will have another 30 seconds to read any data that it requires.(Here is the problem exist before expiring timeout it is again calling reader )如果您再次调用 Read,它将再有 30 秒的时间来读取它需要的任何数据。(这是在超时超时之前存在的问题,它再次调用 reader )

Solution 1 is increasing the connection timeout or setting the value to Zero.解决方案 1 增加连接超时或将值设置为零。 Another Solution might work is use using(var reader = cmd.ExecuteReader();另一个可能有效的解决方案是使用using(var reader = cmd.ExecuteReader();

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

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