我正在尝试从ASP Web API执行存储过程(查询)。我已经开发了它(命令超时为 300 秒)。
一开始,从 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;
}
}
错误消息:
操作完成之前经过的超时期限或服务器没有响应。
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
1 completion, Int32 timeout, Task& task, Boolean 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
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(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
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 command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at HERS_SearchQuery.Data.DataAccess.SQLServer.HERSScheduleRepository.GetScheduledTitles(CriteriaFields _criteria) in C:\Userso\Documents\Visual Studio 2010\Projects\HERS_SchedulerSearch\HERS_SearchQuery.Data\DataAccess\SQLServer
我遇到了类似的问题,我通过使用选项解决了它
使用重新编译
在绞线过程中。碰巧 SQL SERVER 正在缓存一个对所有参数都无效的执行计划。
当然我不知道这是否能解决你的问题,但我认为值得一试。