我有一个属性设置为 1 秒的System.Data.Linq.DataContext
对象,ConnectionTimeout
但连接仍然需要 30 秒才能返回异常。
下面是带有 StopWatch 输出的代码来显示问题:
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 时要修改的错误超时值吗?