我試圖從ASP Web API執行存儲過程(查詢)。我已經開發它(命令超時是300秒)。SqlConnection通過ASP.NET WEB API超時錯誤
開始時,從Web API調用存儲過程的速度非常快,但在調用相同的WEB API幾天之後,它給了我超時錯誤(如下所示)。
同時,如果我嘗試使用相同的參數執行相同的存儲過程,即使同時直接從SQL Server Management Studio中執行,也會在幾秒鐘內執行。
的代碼:
public List<ScheduledTitles> GetScheduledTitles(CriteriaFields _criteria)
{
try
{
System.Data.DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
using (SqlConnection con = new SqlConnection(_strDBConnection))
{
con.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandText = "spQuery_1";
comm.Parameters.Add(new SqlParameter() { ParameterName = "Product_Line_ID", SqlDbType = SqlDbType.Int, Value = _criteria.ProductLineID == null ? (object)System.DBNull.Value : _criteria.ProductLineID });
comm.CommandType = CommandType.StoredProcedure;
da.SelectCommand = comm;
comm.CommandTimeout = 300;
da.Fill(ds);
}
.
.
.
return scheduledTitlesS.ToList<ScheduledTitles>();
}
catch (Exception exc)
{
throw exc;
}
}
錯誤消息:
在操作完成或服務器未響應之前經過超時時間。
在System.Data.SqlClient.SqlConnection.OnError(SqlException異常,布爾breakConnection,行動1 wrapCloseInAction)
1完成的Int32超時,任務&任務,布爾asyncWrite)
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)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource
在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior, RunBehavior runBehavior,布爾returnStream,字符串方法)
在System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior行爲,字符串方法)
在System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior行爲,字符串方法)
在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior行爲)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,DataTable [] datatables,Int32 startRecord,Int32 maxRecords,String srcTable ,IDbCommand的命令,的CommandBehavior行爲)
在System.Data.Common.DbDataAdapter.Fill(數據集的數據集,的Int32 startRecord,的Int32最大記錄,字符串srcTable要,IDbCommand的命令,的CommandBehavior行爲)
在System.Data.Common.DbDataAdapter.Fill (DataSet dataSet)
at C:\ Users \ nah \ Documents \ Visual Studio 2010 \ Projects \ HERS_SchedulerSearch \ HERS_SearchQuery.Data \ DataAccess \ SQLServer中的HERS_SearchQuery.Data.DataAccess.SQLServer.HERSScheduleRepository.GetScheduledTitles(CriteriaFields_criteria)
您是否嘗試過在填充dataadapter後關閉連接? – Tim
就像蒂姆提到的那樣,好像你的關係不關閉;這意味着連接數可能會佔用您的SQL調用。在Try Catch語句中使用con.Close()和finally語句 – sunnysidedown916
請顯示_strDBConnection的外觀。確保「上下文連接= false」 – MikeG