在 ASP.NET 應用程式中使用 DataAdapter 方法時,要求逾時錯誤
本文可協助您解決) ASP.NET Web 應用程式中發生錯誤 (要求超時的問題。
原始產品版本: ASP.NET
原始 KB 編號: 825739
徵狀
當您使用此 DataAdapter.Fill
方法時,或在 ASP.NET 的 Web 應用程式中執行查詢,而該應用程式的處理時間超過90秒時,您可能會收到下列錯誤訊息:
HttpException (0x80004005) :要求超時。
只有當您在發行模式下執行 Web 應用程式,且 Debug
web.config 檔中的屬性值設定為 false時,才會發生此錯誤。
原因
依預設,此屬性的值 executionTimeout
會設定為 Machine.config 檔中的90秒。 當處理時間超過90秒時,便會發生此錯誤。
因應措施
若要解決此問題,請增加設定檔中的屬性設定超時值 executionTimeout
。
此 executionTimeout
屬性存在於 <httpRequest>
Machine.config 檔案中。 您可以在 web.config 檔或 Machine.config 檔案中變更這些設定。 超時的預設值為90秒。 此 executionTimeout
屬性指出在 ASP.NET Web 應用程式關機之前,允許要求執行的最長秒數。
方法1:設定 Web.config 檔案中的 ExecutionTimeout 屬性值
在 [記事本] 中開啟 web.config 檔案。
新增
<httpRuntime>
區段中的元素<system.web>
,如下所示:<configuration> <system.web> <httpRuntime executionTimeout="90" maxRequestLength="4096" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" /> </system.web> </configuration>
修改
executionTimeout
屬性值以避免逾時錯誤。儲存 web.config 檔案。
方法2:設定 Machine.config 檔案中的 ExecutionTimeout 屬性值
在 [記事本] 中開啟 Machine.config 檔案。 Machine.config檔案位於
%SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\
目錄中。在 Machine.config 檔案中,找出該
<httpRuntime>
元素。 web.config檔案位於 Web 應用程式目錄中。<httpRuntime executionTimeout="90" maxRequestLength="4096" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" />
修改
executionTimeout
屬性值以避免逾時錯誤。儲存 Machine.config 檔案。
狀態
產生此錯誤是系統刻意為之。
再現行為的步驟
啟動 Microsoft Visual Studio .NET。
在 [檔案] 功能表上,指向 [新增],然後選取 [專案]。
選取 [專案類型] 底下的 [ Visual Basic 專案],然後選取 [範本] 底下的 [ ASP.NET Web 應用程式] 預設
WebForm1.aspx
會建立。在 [設計檢視] 中,以滑鼠右鍵按一下 [ WebForm1],然後選取 [ view Code]。
若要新增資料庫連接和
DataAdapter
方法來填滿資料集,請使用下列程式碼取代現有的程式碼:Imports System.Data.SqlClient Public Class WebForm1 Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Dim sConnectionString As String sConnectionString = "server=servername;uid=sa;pwd=password;database=testdatabase;" Dim objConn As SqlConnection objConn = New SqlConnection(sConnectionString) objConn.Open() Dim daAuthors As SqlDataAdapter 'Increase the no.of records from existing value, if execution time is less than 90 sec. daAuthors = New SqlDataAdapter("Select top 60000 * From timelog (nolock)", objConn) Dim myDs As DataSet myDs = New DataSet("testTimelog") Dim dt As DateTime dt = New DateTime() dt = dt.Now Response.Write("StartTime of DataAdapter fill - " + dt) daAuthors.Fill(myDs, "testTimelog") dt = New DateTime() dt = dt.Now Response.Write("<br>EndTime of DataAdapter fill - " + dt) Response.Write("<br>No of Rows = " + myDs.Tables(0).Rows.Count.ToString()) Catch ex As Exception Response.Write(ex.ToString()) End Try End Sub End Class
在 [記事本] 中開啟 web.config 檔案,然後將屬性值設
Debug
為 false ,如下所示:<configuration> <system.web> <compilation defaultLanguage="vb" debug="false" /> </system.web> </configuration>
設定要在發行模式中建立的應用程式。 如果要執行這項操作,請依照下列步驟執行:
- 在 [方案 Explorer] 中,以滑鼠右鍵按一下專案。
- 選取 [ 屬性],然後選取 [ Configuration Manager]。
- 選取 [在使用中方案設定] 底下的 [發佈],然後選取 [關閉]。
- 選取 [確定]****。
在 [ 調試 ] 功能表上,選取 [ 開始 建立並執行專案]。 您可能會收到「 徵兆 」一節所述的錯誤訊息。
注意
在 Machine.config 檔中設定的超時預設值為90秒。 如果處理時間小於90秒,請增加要提取的記錄數目,以增加處理時間。