2011-11-02 150 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