簡體   English   中英

連接到SQL Server Express 2012的超時

[英]Timeout connecting to SQL Server Express 2012

我的應用程序是asp.net MVC3,我正在使用SQLExpress2012。出現以下錯誤

超時時間已到。 在操作完成之前超時或服務器沒有響應。

當我嘗試運行以下命令時:

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

我確實將連接時間(服務器/高級屬性)增加到6000。

當我從設計器(在SQL Server中)運行視圖時,會收到相同的錯誤消息,但是,當我運行查詢(在SQL Server中)時,它運行良好,執行了54秒。

非常感謝您的建議,謝謝。

您可能需要設置DBContext的連接時間:

ctx.CommandTimeout = 200;

DataContext類的CommandTimeout的默認值設置為30秒。 任何花費較長時間才能完成的數據庫查詢都將超過30秒(而您寫的時間大約是60秒)將拋出System.Data.SqlClient.SqlException:Timeout expired Exception

如果您查看自動生成的DALDataContext子類,將有一些局部方法聲明,您的關注點應該是OnCreated方法。 您可以使用與自動生成的類相同的全名在另一個局部類中定義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