简体   繁体   English

运行SP超过30秒时超时已到期

[英]Timeout expired when running an SP more than 30 seconds

I have an sp that runs for 45 sec- 1 min but it gives me the error message Timeout expired. 我有一个运行45秒-1分钟的sp但它给我错误消息超时到期。 The timeout period elapsed prior to completion of the operation or the server is not responding. 操作完成之前经过的超时时间或服务器没有响应。 I have add the Connect Timeout=120 to the connection string but that didnt help. 我已将Connect Timeout = 120添加到连接字符串,但这没有帮助。 I've also change the IIS application pool to timeout after 2 mins. 我还在2分钟后将IIS应用程序池更改为超时。

<add name="Oconnection" connectionString="Data Source=servername;Initial Catalog=dmName; User ID=username; Password=pw; Connect Timeout=120" />

This is my cs file: 这是我的cs文件:

  string Oconnection=ConfigurationManager.ConnectionStrings["Oconnection"].ConnectionString;



 public DataSet CreateTable()
    {
        DataSet dsCreateTable;
        dsCreateTable = SqlHelper.ExecuteDataset(Oconnection, CommandType.StoredProcedure, "usp_CreateTables");
        return dsCreateTable;
    }

Do I need to add the timeout in the cs file as well? 我是否还需要在cs文件中添加超时?

Connect Timeout is the time limit for connecting to the sql server. Connect Timeout连接到SQL Server的时间限制。

What you want is CommandTimeout which is the timeout for running a command (query, stored procedure etc) 你想要的是CommandTimeout ,它是运行命令的超时(查询,存储过程等)

https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout?view=netframework-4.7.2

You need to set the CommandTimeout proptery on the SqlCommand object, it is not possible to set in connection string. 您需要在SqlCommand对象上设置CommandTimeout proptery,不能在连接字符串中设置。

You can use it in your code like this: 您可以在代码中使用它,如下所示:

public DataSet CreateTable()
{
    using(var conn = new SqlConnection(Oconnection))
    {
       conn.Open();
       using(var command = new SqlCommand())
       {
          command.Connection = conn;
          command.CommandTimeout = 120; // higher if needed
          command.CommandType = CommandType.StoredProcedure;
          command.CommandText = "usp_CreateTables";
          var da = new SqlDataAdapter();
          da.SelectCommand = command;
          var ds = new DataSet();
          da.Fill(ds);

          return ds;
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM