2011-11-02 70 views
8

當我在web應用程序中運行查詢時,我得到一個null值。直接在SQL Management Studio中直接進行查詢將返回結果。如何增加web.config中執行sql查詢的時間

我認爲問題是超時。如何增加在Web應用程序中執行查詢的時間?在我的web.config:connectionstring,沒有超時代碼。如果我在那裏選擇超時,是否會影響我係統的其他部分?

+1

它不能由配置來控制。設置SqlCommand的CommandTimeout。有關頁面請求超時設置的更多信息,請參閱http://stackoverflow.com/questions/7804622/how-to-upload-content-more-than-2-mbs-on-website-created-using-asp-net -4-0/7804670#7804670 – Prasanth

+1

你怎麼知道它是因爲暫停?嘗試使用Sql Profiler並查看正在形成的查詢。查看其他詳細信息,如持續時間,讀取等。從探查器中提取查詢,然後在Sql Server中執行該查詢。 –

+0

Iam使用sql server 2005 management studio.Where是profiler.I不知道如何使用它? –

回答

4

您應該添加httpRuntime塊並處理executionTimeout(以秒爲單位)。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
... 
<system.web> 
    <httpRuntime executionTimeout="90" maxRequestLength="4096" 
    useFullyQualifiedRedirectUrl="false" 
    minFreeThreads="8" 
    minLocalRequestFreeThreads="4" 
    appRequestQueueLimit="100" /> 
</system.web> 
... 
</configuration> 

欲瞭解更多信息,請參閱msdn page

+1

這是執行ASP.NET頁面本身的超時,而不是SQL查詢。 – Abel

+1

如果您將查詢作爲請求的一部分運行,則httpRuntime executionTimeout應該至少與查詢執行超時一樣長,否則請求將超時並在查詢有機會完成之前取消。 – Triynko

1

我認爲你不能增加查詢執行的時間,但你需要增加請求的超時時間。

Execution Timeout Specifies the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET. (Default time is 110 seconds.)

有關詳細信息,請看看https://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx

您可以在web.config做。 e.g

<httpRuntime maxRequestLength="2097152" executionTimeout="600" /> 
3

SQL Server沒有設置控制查詢超時連接字符串中,而據我所知,這是對其他主要的數據庫相同。但是,這並不像你所看到的問題:我希望看到一個異常引發

Error: System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

如果是真正執行查詢超時。

如果這確實是一個問題,您可以將SQL Server數據庫的默認超時值作爲數據庫本身的屬性進行更改;爲此使用SQL Server管理器。

請確保查詢是,確切地說與您正在運行的Web應用程序相同。使用分析器來驗證這一點。

9

你可以做一件事。

  1. 在AppSettings.config(創建一個如果不存在),創建一個鍵值對。
  2. 在代碼中,拉取值並將其轉換爲Int32並將其分配給command.TimeOut。

,如: - 在appsettings.config - >

<appSettings>  
    <add key="SqlCommandTimeOut" value="240"/> 
</appSettings> 

代碼 - >

command.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]); 

應該這樣做。

注意: - 我在使用Microsoft應用程序塊中的SqlHelper類時遇到了大多數超時問題。如果你的代碼中有它,並且面臨超時問題,那麼你最好使用sqlcommand並按照上面的描述設置它的超時時間。對於所有其他情況sqlhelper應該沒問題。如果你的客戶可以等待比sqlhelper類提供的更長的時間,你可以繼續使用上述技術。

例如: - 使用此 -

SqlCommand cmd = new SqlCommand(completequery); 

cmd.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]); 

SqlConnection con = new SqlConnection(sqlConnectionString); 
SqlDataAdapter adapter = new SqlDataAdapter(); 
con.Open(); 
adapter.SelectCommand = new SqlCommand(completequery, con); 
adapter.Fill(ds); 
con.Close(); 

而不是

DataSet ds = new DataSet(); 
ds = SqlHelper.ExecuteDataset(sqlConnectionString, CommandType.Text, completequery); 

更新:另請參閱下面@Triynko回答。檢查這一點也很重要。

+0

這將幫助您隨時更改值。但是,如果您正在規劃龐大的數據,我建議您對業務性質以及數據如何積累進行更多分析,然後計劃您的Db設計。由於所需的超時時間可能隨着記錄數量和複雜程度的增加而增加。如果您已經創建了一個數據庫或者不允許他設計,請放棄此評論。 – 2015-04-24 06:44:36

0

我意識到我是一個遲到的遊戲,但只花了一天的時間試圖改變web服務的超時。它似乎有30秒的默認超時時間。我改變埃夫裏的其他超時值後,我能找到,其中包括:

  • DB連接字符串連接超時
  • 的httpRuntime executionTimeout
  • basicHttpBinding的約束力closeTimeout
  • basicHttpBinding的結合的SendTimeout
  • basicHttpBinding的約束力receiveTimeout
  • basicHttpBinding binding openTimeout

Finaley我發現這是SqlCommand超時默認爲30秒。

我決定只是將連接字符串的超時時間複製到命令中。 連接字符串在web.config中配置。

某些代碼:

namespace ROS.WebService.Common 
{ 
    using System; 
    using System.Configuration; 
    using System.Data; 
    using System.Data.SqlClient; 

    public static class DataAccess 
    { 
    public static string ConnectionString { get; private set; } 

    static DataAccess() 
    { 
     ConnectionString = ConfigurationManager.ConnectionStrings["ROSdb"].ConnectionString; 
    } 

    public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams) 
    { 
     using (SqlConnection conn = new SqlConnection(DataAccess.ConnectionString)) 
     { 
     using (SqlCommand cmd = new SqlCommand(cmdText, conn) { CommandType = cmdType, CommandTimeout = conn.ConnectionTimeout }) 
     { 
      foreach (var p in sqlParams) cmd.Parameters.Add(p); 
      cmd.Connection.Open(); 
      return cmd.ExecuteNonQuery(); 
     } 
     } 
    } 
    } 
} 

更改引入到 「重複」 從連接字符串的超時值:的CommandTimeout = conn.ConnectionTimeout