ASP.NET C# SqlDataSource超时问题

11

我正在尝试将SqlDataSource的超时时间延长到30秒以上(似乎是默认值)。我正在运行一个必须遍历数十万条记录的存储过程。在繁忙期间,它会超时。我正在使用ASP.NET 4.0和2003服务器上的IIS 6.0。

错误信息: 超时已过期。操作完成之前超时时间已过或服务器未响应。

我已经尝试了无效的超时时间延长方法:

< asp:SqlDataSource ID="dsTest" EnableCaching="true" CacheDuration="604800" runat="server" ConnectionString="<%$ ConnectionStrings:SuperNARIC %>" SelectCommand="selectStatus" SelectCommandType="StoredProcedure" onselecting="dsTest_Selecting" >
    <SelectParameters>
        < asp:ControlParameter ControlID="ddlCar" Name="CountryID" PropertyName="SelectedValue" Type="Int32" />
    < /SelectParameters>
< /asp:SqlDataSource>



protected void dsTest_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        e.Command.CommandTimeout = 300;
    }

任何帮助都将不胜感激。
谢谢。
8个回答

16

此处所述,这对我有用。

您可以像这样增加超时属性

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.CommandTimeout = 0;
        }

将超时时间设置为0表示没有超时限制


10

有两种类型的超时: ConnectionTimeoutCommandTimeout

ConnectionTimeout 确定您的应用程序等待建立与服务器的连接的最长时间。

CommandTimeout: 允许命令执行的最长时间。

确保您同时设置这两个超时。在 Comand 中:

 command.CommandTimeout = 300;

注意: 如果您的命令是数据源的一部分,则可在 Selecting 事件中实现此功能。 e.Command.CommandTimeout = 0; 值为 0 表示无限等待。
另外,连接字符串:
SqlConnectionStringBuilder cs = new SqlConnectionStringBuilder(connectionString);
cs.ConnectTimeout = 300;

或者:

<add name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS; Database=MyDB; Integrated Security=True;Pooling=True;connection timeout=30" providerName="System.Data.SqlClient" />

注意:尝试在全局范围内设置连接字符串超时时间,可以在配置文件中进行设置。


嗨,我在我的 web.config 文件中有以下内容:连接超时=180。 - user1024416
酷,为了测试目的,将其增加到500并查看。 - Ulises

3

你好,我在我的web.config文件中有以下内容:连接超时=180。 - user1024416

1

您需要确保将CommandTimeoutConnectionStringConnect Timeout都设置好,以防止长时间运行的存储过程超时。如果您不设置连接超时,即使存储过程命令本身没有超时,您也会在存储过程完成之前超时。


感谢您的快速回复。我在我的web.config文件中有以下内容:连接超时=180。 - user1024416

1
这是对我有效的方法:

  <script runat="server">
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
    e.Command.CommandTimeout = 0;
    }
    </script>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="Your Connection String" 
ProviderName="Teradata.Client.Provider"
SelectCommand="A SELECT COMMAND THAT TAKES A LONG TIME"
DataSourceMode="DataSet"
onselecting="SqlDataSource1_Selecting">

0

最大连接超时值可以是2147483647。请尝试在您的Web Config中将连接超时值设置为以下内容:

<connectionStrings> <add name="ConnectionString" connectionString="Data Source=144681;Initial Catalog=Customer;Persist Security Info=True;User ID=xxxxx;Password=yyyyy" providerName="System.Data.SqlClient" /> </connectionStrings>


1
<add name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS; Database=MyDB; Integrated Security=True;Pooling=True;connection timeout=30" providerName="System.Data.SqlClient" /> 添加到 <connectionStrings></connectionStrings> 中。 - Akshay Khandelwal

0

感谢您的快速回复。我在我的web.config文件中有以下内容:连接超时=180。 - user1024416

-1

SQL查询和页面响应时间都有超时限制。

Server.ScriptTimeout = 120; 
e.Command.CommandTimeout = 120;

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