如何在web.config中增加执行SQL查询的时间

13

当我在Web应用程序中运行查询时,返回一个null值。而在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查看正在生成的查询语句。查看其他细节,例如持续时间、读取次数等等。从Profiler中获取查询语句,然后在Sql Server中执行该查询。 - Pawan Mishra
我正在使用SQL Server 2005管理工具。Profiler在哪里?我不知道如何使用它? - Jui Test
5个回答

13

你可以做一件事情。

  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"]);

应该可以了。

注意:- 当我使用来自微软应用程序块的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的回答。那也很重要。


这将帮助您在需要时更改值。但是,如果您计划处理大量数据,我建议对业务性质和数据可能积累的方式进行更多分析,然后规划您的数据库设计。因为所需的超时时间可能会随着记录数量的增加和涉及的复杂性而增加。如果您已经创建了数据库或不允许进行架构设计,请忽略此评论。 - user3328730

3
我知道我来晚了,但是花了一天时间尝试更改webservice的超时时间。它似乎默认为30秒。我更改了所有其他超时值,包括:
- DB连接字符串连接超时 - httpRuntime executionTimeout - basicHttpBinding binding closeTimeout - basicHttpBinding binding sendTimeout - basicHttpBinding binding receiveTimeout - basicHttpBinding binding openTimeout
最终我发现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


3

SQL Server没有在连接字符串中控制查询超时的设置,据我所知其他主要数据库也是如此。但是,这似乎不是您遇到的问题:如果真的有超时执行查询,我会期望看到引发异常。

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

如果确实出现了问题,您可以将SQL Server数据库的默认超时更改为数据库本身的属性;使用SQL Server Manager进行此操作。

请确保来自Web应用程序的查询与您直接运行的查询完全相同。使用分析器验证此内容。


SQL Management Studio的设置是否仍然只针对自身而不是针对SQL Server?(我怀疑是这样的) - symbiont

2
您应该添加 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页面


4
这是ASP.NET页面本身执行的超时时间,而不是SQL查询的超时时间。 - Abel
1
httpRuntime的executionTimeout应该至少与查询执行超时时间一样长,如果您将查询作为请求的一部分运行,则请求会在查询有机会完成之前超时并被取消。 - Triynko

1

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

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

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

你可以在 web.config 中进行设置。例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接