简体   繁体   English

连接到SQL Server Express 2012的超时

[英]Timeout connecting to SQL Server Express 2012

my application is asp.net MVC3, I am using SQLExpress 2012. I get the following error 我的应用程序是asp.net MVC3,我正在使用SQLExpress2012。出现以下错误

Timeout expired. 超时时间已到。 The timeout period elapsed prior to completion of the operation or the server is not responding. 在操作完成之前超时或服务器没有响应。

when I try to run the following: 当我尝试运行以下命令时:

public static List<vw_MasterView> GetMasterView(DateTime? fromDate, DateTime? toDate)
{
    if (fromDate == null) fromDate = new DateTime(1900, 1, 1);
    if (toDate == null) toDate = DateTime.Now;
    using (DALDataContext ctx = new DALDataContext())
    {
        var q = from c in ctx.vw_MasterViews
                where c.FirstVisitDate >= fromDate && c.LastVisitDate <= toDate
                select c;
        return q.ToList();
    }
} 

I did increase the connection time (server / advance properties) to 6000. 我确实将连接时间(服务器/高级属性)增加到6000。

When I run the view from the designer (in SQL Server) I get the same error message, however when I run the query (in SQL server) it works fine, it took 54 seconds to excute. 当我从设计器(在SQL Server中)运行视图时,会收到相同的错误消息,但是,当我运行查询(在SQL Server中)时,它运行良好,执行了54秒。

I would appreciate your suggestions, thanks in advance. 非常感谢您的建议,谢谢。

您可能需要设置DBContext的连接时间:

ctx.CommandTimeout = 200;

The default value of the DataContext class's CommandTimeout is set to 30 seconds. DataContext类的CommandTimeout的默认值设置为30秒。 Any database queries taking a longer time to complete than 30 seconds (and as you wrote your is taking about 60) will throw a System.Data.SqlClient.SqlException: Timeout expired Exception . 任何花费较长时间才能完成的数据库查询都将超过30秒(而您写的时间大约是60秒)将抛出System.Data.SqlClient.SqlException:Timeout expired Exception

If you look at the auto generated DALDataContext subclass you will a few partial method declarations, your point of interest should be OnCreated method. 如果您查看自动生成的DALDataContext子类,将有一些局部方法声明,您的关注点应该是OnCreated方法。 You can define the body of the OnCreated method in another partial class with the same full name as that of the auto generated class and set the desired timeout value there in the following way: 您可以使用与自动生成的类相同的全名在另一个局部类中定义OnCreated方法的主体,并通过以下方式在其中设置所需的超时值:

partial class DALDataContext : System.Data.Linq.DataContext
{
    partial void OnCreated()
    {
        this.CommandTimeout = 100;
    }
}

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

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