【问题标题】:Wait operation time out mvc等待操作超时mvc
【发布时间】:2013-07-05 03:06:26
【问题描述】:

我有一个问题:如何处理asp.net MVC页面中的操作超时。在加载查询数据库 SQL Server 的页面时,它会加载产品。有时它会说等待操作超时。如何处理。

【问题讨论】:

  • 检查你的代码。检查你的 SQL 查询,它占用了多少 CPU 和资源。

标签: c# asp.net-mvc-3 sql-server-2008


【解决方案1】:

为了避免在运行很长的查询时出现等待操作超时异常,请更改 SQL 命令属性并允许命令在调用超时之前等待更长的时间。

using System;
using System.Data.SqlClient;
/// 
public class A {
  /// 
  public static void Main() {
     string connectionString = "";
     // Wait for 5 second delay in the command
     string queryString = "waitfor delay '00:00:05'";
     using (SqlConnection connection = new SqlConnection(connectionString)) {
       connection.Open();
       SqlCommand command = new SqlCommand(queryString, connection);
       // Setting command timeout to 1 second
        command.CommandTimeout = 1;
          try {
              command.ExecuteNonQuery();
              }
           catch (SqlException e) {
              Console.WriteLine("Got expected SqlException due to command timeout ");
              Console.WriteLine(e);
      }
   }
}

}

执行 Linq to sql 查询可能需要更长的时间并超过 DataContext 类的 CommandTimeout 属性的默认值。 默认值为 30 秒。 我们可以在每次创建 LINQ to SQL DataContext 对象并调用查询之前设置 CommandTimeout 的值。

 #region Constructors

    /// <summary>
    /// Initializes a new FrameworkEntities object using the connection string found in the 'FrameworkEntities' section of the application configuration file.
    /// </summary>
    public FrameworkEntities() : base("name=FrameworkEntities", "FrameworkEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        this.CommandTimeout = 300;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new FrameworkEntities object.
    /// </summary>
    public FrameworkEntities(string connectionString) : base(connectionString, "FrameworkEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        this.CommandTimeout = 300;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new FrameworkEntities object.
    /// </summary>
    public FrameworkEntities(EntityConnection connection) : base(connection, "FrameworkEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        this.CommandTimeout = 300;
        OnContextCreated();
    }

    #endregion

【讨论】:

  • 零是无限超时。当来自 SQL SERVER 实例的响应被分解为多个网络数据包时,连接将被错误地关闭并发生一般网络错误。
  • 您好 AnandMohanAwasthi 先生,我正在使用 LINQ 语句。上面的代码(command.CommandTimeout =1)应该怎么改?
猜你喜欢
  • 2015-02-02
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多