当我在Web应用程序中运行查询时,我得到null
值。 SQL Management Studio中直接相同的查询返回结果。
我认为问题是暂停。如何在Web应用程序中增加执行查询的时间?在我的web.config:connectionstring
中,没有超时代码。如果我在那里选择超时,会影响我系统的其他部分吗?
答案 0 :(得分:11)
你可以做一件事。
如: - 在appsettings.config - >
<appSettings>
<add key="SqlCommandTimeOut" value="240"/>
</appSettings>
代码 - &gt;
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答案。重要的是要检查一下。
答案 1 :(得分:3)
您应该添加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。
答案 2 :(得分:3)
SQL Server没有设置来控制连接字符串中的查询超时,据我所知,这与其他主要数据库相同。但是,这看起来不像你看到的问题:我希望看到一个例外提出
错误:System.Data.SqlClient.SqlException:超时已过期。操作完成之前经过的超时时间或服务器没有响应。
如果确实有超时执行查询。
如果这确实是一个问题,您可以将SQL Server数据库的默认超时更改为数据库本身的属性;使用SQL Server Manager进行此操作。
确保您的Web应用程序中的与完全的查询与您直接运行的查询相同。使用分析器验证这一点。
答案 3 :(得分:1)
我认为您无法增加查询执行的时间,但您需要增加请求的超时时间。
执行超时指定在自动关闭之前允许请求执行的最大秒数 ASP.NET。 (默认时间是110秒。)
有关详细信息,请查看https://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx
您可以在web.config中执行此操作。 e.g
<httpRuntime maxRequestLength="2097152" executionTimeout="600" />
答案 4 :(得分:1)
我意识到我是游戏后期的傻瓜,但只花了一天时间试图改变网络服务的超时。它的默认超时时间似乎为30秒。我改变了evry其他超时值后我可以找到,包括:
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();
}
}
}
}
}
引入的变更为&#34;重复&#34;连接字符串中的超时值: CommandTimeout = conn.ConnectionTimeout