简体   繁体   中英

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():

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

How long it takes for your 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).

The default is 30 seconds. if Read requires two network packets, then it has 30 seconds to read both network packets. 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 )

Solution 1 is increasing the connection timeout or setting the value to Zero. Another Solution might work is use using(var reader = cmd.ExecuteReader();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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