【问题标题】:Adding Command Timeout in .Net Aspx Page在 .Net Aspx 页面中添加命令超时
【发布时间】:2016-07-19 00:06:45
【问题描述】:

我有一个 .aspx 页面,它只是使用 SQL 查询和网格在页面中显示结果。我没有后面的代码,它只是 aspx 页面。 如何在 aspx 页面或 web.config 中添加命令超时?在 web.config 中,我知道我可以在连接字符串中添加连接超时作为连接超时 = 1000,但是如何添加命令超时,以便在选择下拉参数打开页面时不会收到超时过期消息。我的查询似乎需要很长时间才能运行,我需要增加命令超时时间。

我收到的超时消息:

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

超时。在完成之前超时时间已过 操作或服务器没有响应。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.Data.SqlClient.SqlException:超时 已到期。在完成之前超时时间已过 操作或服务器没有响应。

[SqlException (0x80131904): 超时。超时时间 在操作完成之前已经过去,或者服务器没有 回应。]
System.Data.SqlClient.SqlConnection.OnError(SqlException 异常, 布尔中断连接)+212
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +245 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior、SqlCommand cmdHandler、SqlDataReader 数据流、 BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1099
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +58
System.Data.SqlClient.SqlDataReader.get_MetaData() +112
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6319508
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior runBehavior、布尔 returnStream、布尔 异步)+6320577
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior、runBehavior、布尔返回流、字符串 方法,DbAsyncResult 结果)+424
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior、runBehavior、布尔返回流、字符串 方法)+28
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)+211
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 行为)+19
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior 行为)+19 System.Data.Common.DbDataAdapter.FillInternal(DataSet 数据集,DataTable[] 数据表,Int32 startRecord,Int32 maxRecords, 字符串 srcTable、IDbCommand 命令、CommandBehavior 行为)+221
System.Data.Common.DbDataAdapter.Fill(数据集数据集,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand 命令, 命令行为行为)+573
System.Data.Common.DbDataAdapter.Fill(数据集数据集,字符串 srcTable) +161
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments 论据)+2803174
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback 回调)+27
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +46
System.Web.UI.Control.PreRenderRecursiveInternal() +108
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +3394

【问题讨论】:

  • 你能告诉我们确切的超时信息吗?
  • 我在我提出的问题中添加了确切的超时消息。
  • 我更新了答案。
  • SqlCommand command = new SqlCommand(queryString, connection); command.CommandTimeout = 600;如果有的话,这需要添加到代码隐藏中吗?就我而言,后面没有代码,它只是带有网格的 aspx 页面。和我在 web.config 中添加的方式不一样吗?
  • 如果你的 Web.config 中有连接字符串,你可以添加Timeout=600;Connect Timeout=600; 到它(不确定哪个好,我在不同的地方都看到了)。跨度>

标签: c# asp.net


【解决方案1】:

你可以在Web.config中增加executionTimeout:

<configuration>
    <system.web>
        <httpRuntime executionTimeout="600" ... />
    </system.web>
</configuration>

值以秒为单位。

更新

根据您的异常消息,一种解决方案是增加 SQLCommand 对象的超时时间:

SqlCommand command = new SqlCommand(queryString, connection);
command.CommandTimeout = 600;

这里给出了类似问题的其他建议:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated

【讨论】:

  • 我在 web.config 中添加了 并重新启动了 IIS。仍然超时发生。在 SQL 服务器端,运行查询只需要 9 秒。其他下拉参数选择适用于较少的数据,但只有一个选择不起作用并显示超时过期消息。我也做了
猜你喜欢
  • 2023-04-09
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多