2017-08-07 90 views
-1

我很抱歉,因爲標題太籠統。我只是想知道這個代碼如何發生:ExecuteReader執行超時時循環過期

foreach (var item in list) 
{ 
    ......... 
    using (SqlCommand cmd = new SqlCommand(@"SELECT some_fields FROM tbl WHERE [email protected]", 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(); 
    } 
    ......... 
} 

執行超時發生在第二個循環。第一個循環沒有問題。這個語法有什麼問題嗎?請告訴我。

異常時的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 

在此先感謝

+2

是的,它的代碼錯誤,你沒有分享。循環部分是什麼? 「做點什麼」可能需要幾年時間。 – Reniuz

+0

嗯,但異常發生在var reader = cmd.ExecuteReader(); @Reniuz。其實它只是從結果查詢當然設置了一些變量。 – andrefadila

+0

請添加例外詳情。消息,堆棧跟蹤的相關部分 – Reniuz

回答

0

嘗試增加連接超時

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

ExecuteReader()

花費多長時間做這個您的cmd.ExecuteReader();執行TE?

0

你的連接字符串,你可能想通過允許多個活動結果集來嘗試。

看看這是否能解決您的問題?像:

<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" /> 
0

值0表示沒有限制(試圖執行一個命令將無限期地等待)。

默認值是30秒。如果Read需要兩個網絡數據包,則它需要30秒才能讀取兩個網絡數據包。如果再次調用Read,則需要30秒的時間來讀取它需要的任何數據(這是在超時過期之後存在的問題,它再次呼叫閱讀器)

解決方案1增加了連接超時或設置值到零 另一個解決方案可能會工作是使用using(var reader = cmd.ExecuteReader();