我很抱歉,因爲標題太籠統。我只是想知道這個代碼如何發生: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
在此先感謝
是的,它的代碼錯誤,你沒有分享。循環部分是什麼? 「做點什麼」可能需要幾年時間。 – Reniuz
嗯,但異常發生在var reader = cmd.ExecuteReader(); @Reniuz。其實它只是從結果查詢當然設置了一些變量。 – andrefadila
請添加例外詳情。消息,堆棧跟蹤的相關部分 – Reniuz