Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SP运行超过30秒时超时_C#_Asp.net - Fatal编程技术网

C# SP运行超过30秒时超时

C# SP运行超过30秒时超时,c#,asp.net,C#,Asp.net,我有一个sp运行45秒-1分钟,但它给我的错误消息超时已过期。操作完成前已过超时时间,或者服务器没有响应。我已经将connecttimeout=120添加到连接字符串中,但这没有帮助。我还将IIS应用程序池更改为2分钟后超时 <add name="Oconnection" connectionString="Data Source=servername;Initial Catalog=dmName; User ID=username; Password=pw; Connect Timeou

我有一个sp运行45秒-1分钟,但它给我的错误消息超时已过期。操作完成前已过超时时间,或者服务器没有响应。我已经将connecttimeout=120添加到连接字符串中,但这没有帮助。我还将IIS应用程序池更改为2分钟后超时

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

我是否也需要在cs文件中添加超时

连接超时是连接到sql server的时间限制

您需要的是
CommandTimeout
,它是运行命令(查询、存储过程等)的超时

您需要在
SqlCommand
对象上设置
CommandTimeout
属性,无法在连接字符串中设置

您可以在代码中使用它,如下所示:

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;
      }
   }
}

这是建立连接的超时时间(因此得名)。命令超时只能在相关中设置:您唯一的选择是在
ExecuteDataset
中更改代码。在该代码的某个地方有一个
SqlCommand
对象。该对象具有
CommandTimeout
属性。我猜您的变量Oconnection只包含连接字符串?我编辑了代码来创建一个新的连接。更多的例子请看这里的答案:现在我为您编写了代码。当然我没有测试过,因为我没有数据库。没问题。我祝愿您在学习c#/.NET方面取得更大进展。
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;
      }
   }
}