首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >等待操作超时mvc

问等待操作超时mvc
EN

Stack Overflow用户
提问于 2013-07-05 11:06:27
回答 1查看 12K关注 0票数 2

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

EN

回答 1

Stack Overflow用户

发布于 2013-07-05 11:45:24

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

代码语言:javascript
复制
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的值。

代码语言:javascript
复制
 #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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17480509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档