【发布时间】:2013-07-02 03:03:40
【问题描述】:
我有一个 System.Data.Linq.DataContext 对象,其 ConnectionTimeout 属性设置为 1 秒,但连接仍需要 30 秒才能返回异常。
这是显示问题的秒表输出代码:
Debug.WriteLine("!! Method start");
Stopwatch sw = Stopwatch.StartNew();
MyDataClassesDataContext dc = new MyDataClassesDataContext();
Debug.WriteLine("!! Connection.ConnectionTimeout is: " + dc.Connection.ConnectionTimeout);
// Output is: !! Connection.ConnectionTimeout is: 1
Debug.WriteLine("!! CommandTimeout is: " + dc.CommandTimeout);
// Output is: !! CommandTimeout is: 1
try
{
string s = (from rows in dc.MyTable
where rows.MyValue == 3
select rows.MySecondValue).SingleOrDefault();
return s;
}
catch (Exception)
{
sw.Stop();
Debug.WriteLine("!! Method ended after " + sw.Elapsed.Seconds + " seconds");
// Output is: !! Method ended after 27 seconds
return null;
}
ConnectionTimeout 属性使用连接字符串设置为 1。在连接之前它似乎是 1,所以它应该在 1 秒后超时,但需要 27 秒。这是使用 LINQ to SQL 时要修改的错误超时值吗?
【问题讨论】:
-
我删除了我的答案,因为我认为这是 EF(我应该更多地查看标签)。你在哪里设置你的 DataContext 命令超时?
-
我在 OnCreated 方法中执行此操作:partial void OnCreated() { this.CommandTimeout = 1; }
-
异常是否与连接有关?因为那将是您需要更改的
ConnectionTimeout(我知道这很明显,只需要知道)。 -
是的,异常是 System.Data.SqlClient.SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)---> System.ComponentModel.Win32Exception (0x80004005):找不到网络路径
-
您将
CommandTimeout设置为1,但没有发送任何命令。等待时间与ConnectionTimeout(我相信默认为30)有关。
标签: c# asp.net linq-to-sql