【问题标题】:How to increase time in web.config for executing sql query如何增加 web.config 中执行 sql 查询的时间
【发布时间】:2011-11-02 06:54:55
【问题描述】:

当我在 Web 应用程序中运行查询时,我得到一个 null 值。直接在 SQL Management Studio 中的相同查询返回结果。

我认为问题是超时。如何增加在 Web 应用程序中执行查询的时间?在我的 web.config 中:connectionstring,没有超时代码。如果我在那里选择超时,这会影响我系统的其他部分吗?

【问题讨论】:

  • 无法通过配置控制。设置 SqlCommand 的 CommandTimeout。如需了解有关页面请求超时设置的更多信息,请参阅帖子stackoverflow.com/questions/7804622/…
  • 超时怎么知道?尝试使用 Sql Profiler 并查看正在形成的查询。查看其他详细信息,例如持续时间、读取次数等。从探查器中提取查询,然后在 Sql Server 中执行该查询。
  • 我正在使用 sql server 2005 management studio。profiler 在哪里。我不知道如何使用它?

标签: asp.net sql web-config connection-string


【解决方案1】:

你可以做一件事。

  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 答案。检查这一点也很重要。

【讨论】:

  • 这将帮助您随时更改值。但是如果你正在规划海量数据,我建议你对业务的性质和数据如何积累做更多的分析,然后规划你的 Db 设计。因为所需的超时可能会随着记录数量和所涉及的复杂性的增加而增加。如果您已经创建了数据库或不允许构建数据库,请放弃此评论。
【解决方案2】:

SQL Server 没有设置来控制连接字符串中的查询超时,据我所知,其他主要数据库也是如此。但是,这看起来不像您看到的问题:我希望看到引发异常

错误:System.Data.SqlClient.SqlException:超时已过期。在操作完成之前超时时间已过或服务器没有响应。

如果确实存在执行查询的超时。

如果这确实是个问题,您可以将 SQL Server 数据库的默认超时更改为数据库本身的属性;为此使用 SQL Server 管理器。

确保您的 Web 应用程序中的查询与您直接运行的查询完全相同。使用分析器来验证这一点。

【讨论】:

  • SQL Management Studio 中的设置是否仍然为自身而不是 SQL Server? (我怀疑是这样)
【解决方案3】:

我意识到我玩游戏有点晚了,但我花了一天多的时间尝试更改 Web 服务的超时。它似乎有 30 秒的默认超时。我在更改了我能找到的所有其他超时值后,包括:

  • 数据库连接字符串连接超时
  • httpRuntime executionTimeout
  • basicHttpBinding 绑定 closeTimeout
  • basicHttpBinding 绑定 sendTimeout
  • basicHttpBinding 绑定接收超时
  • basicHttpBinding 绑定 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

【讨论】:

    【解决方案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

    【讨论】:

    • 这是执行 ASP.NET 页面本身的超时时间,而不是 SQL 查询的超时时间。
    • 如果您将查询作为请求的一部分运行,httpRuntime executionTimeout 可能至少与查询执行超时一样长,否则请求将超时并在查询出现之前被取消有机会完成。
    【解决方案5】:

    我认为你不能增加查询执行的时间,但你需要增加请求的超时时间。

    Execution Timeout 指定请求在被自动关闭之前允许执行的最大秒数 ASP.NET。 (默认时间为 110 秒。)

    详情请看https://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx

    你可以在 web.config 中做。例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多