C# 通过ASP.NET WEB API的SqlConnection超时错误
我正在尝试从ASP Web API执行存储过程查询。我开发的命令超时时间是300秒 一开始,从WebAPI调用存储过程非常快,但是在调用同一个WebAPI几天后,它给了我下面所示的超时错误 同时,如果我尝试直接从SQLServerManagementStudio使用相同的参数同时执行相同的存储过程,它将在几秒钟内执行 守则:C# 通过ASP.NET WEB API的SqlConnection超时错误,c#,sql-server,asp.net-web-api,sqlconnection,sqlclient,C#,Sql Server,Asp.net Web Api,Sqlconnection,Sqlclient,我正在尝试从ASP Web API执行存储过程查询。我开发的命令超时时间是300秒 一开始,从WebAPI调用存储过程非常快,但是在调用同一个WebAPI几天后,它给了我下面所示的超时错误 同时,如果我尝试直接从SQLServerManagementStudio使用相同的参数同时执行相同的存储过程,它将在几秒钟内执行 守则: public List<ScheduledTitles> GetScheduledTitles(CriteriaFields _criteria) {
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.OneErrorSqlException异常、布尔断开连接、Action1 wrapCloseInAction
在System.Data.SqlClient.TdsParser.throwexception和warningdsparserstateObject stateObjectStateObj中,调用布尔连接锁,布尔异步关闭
位于System.Data.SqlClient.TdsParser.TryRunRunBehavior runBehavior、SqlCommand cmdHandler、SqlDataReader dataStream、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、Boolean和dataReady
位于System.Data.SqlClient.SqlDataReader.tryconsumedata
位于System.Data.SqlClient.SqlDataReader.get_元数据
在System.Data.SqlClient.SqlCommand.FinishExecuteReaderSqlDataReader ds中,运行行为RunBehavior,字符串重置选项String
位于System.Data.SqlClient.SqlCommand.RunExecuteReaderTsCommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务和任务、Boolean asyncWrite、SqlDataReader ds
在System.Data.SqlClient.SqlCommand.RunExecuteReaderCommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、String方法、TaskCompletionSource1 completion、Int32 timeout、Task&Task、Boolean asyncWrite
位于System.Data.SqlClient.SqlCommand.RunExecuteReaderCommandBehavior cmdBehavior,RunBehavior RunBehavior,Boolean returnStream,String方法
在System.Data.SqlClient.SqlCommand.ExecuteReaderCommandBehavior中,字符串方法
在System.Data.SqlClient.SqlCommand.ExecuteReaderCommandBehavior中,字符串方法
位于System.Data.SqlClient.SqlCommand.ExecuteDbDataReaderCommandBehavior
位于System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReaderCommandBehavior
位于System.Data.Common.DbDataAdapter.FillInternalDataSet数据集、DataTable[]datatables、Int32 StartRecords、Int32 maxRecords、String srcTable、IDbCommand命令、CommandBehavior
位于System.Data.Common.DbDataAdapter.FillDataSet数据集、Int32 startRecord、Int32 maxRecords、String srcTable、IDbCommand命令、CommandBehavior
位于System.Data.Common.DbDataAdapter.FillDataSet数据集
在C:\Users\nah\Documents\Visual Studio 2010\Projects\HERS\u SchedulerSearch\HERS\u SearchQuery.Data.DataAccess.SQLServer.HERSScheduleRepository.GetScheduledTitlesCriteriaFields\u条件下
几年前,我遇到了一个类似的问题,我通过使用选项解决了它 重新编译 在strored程序中。SQL SERVER缓存的执行计划并非对所有参数都有效
当然,我不知道这是否能解决您的问题,但我认为值得一试。在填充dataadapter后,您是否尝试过关闭连接?正如Tim提到的,您的连接似乎没有关闭;这意味着连接的数量可能会占用SQL调用。在Try-Catch语句中使用con.Close和finally,请显示_-strDBConnection的外观。确保上下文连接=false@Tim:我不需要显式关闭连接,因为使用SqlConnection con=new SqlConnection\u strDBConnection块将转换为普通try。。在finally中调用IDisposable对象的编译器最终阻塞。首先,去掉try/catch块。它除了使它看起来像是来自抛出的异常之外没有任何效果。第二,请将您的SqlCommand和SqlDataAdapter放入使用块中,看看会发生什么。