首页 > 解决方案 > ADO.Net 查询突然停止处理我的查询

问题描述

我在 ASP.Net 应用程序中使用 ADO.Net。突然我的查询停止工作。这是原始查询:

using (var sqlConnection = new SqlConnection(this.connection))
{
    var sql = @"select *
                From Contacts.Departments a
                Left Outer Join Contacts.ContactMapping b on a.DepartmentID = b.DepartmentID
                Left Outer Join Contacts.Contacts c on b.ContactID = c.ContactID
                Left Outer Join Contacts.Divisions d on b.DivisionID = d.DivisionID
                Left Outer Join Contacts.Emails e on b.EmailID = e.EmailID
                Left Outer Join Contacts.Phones f on b.PhoneID = f.PhoneID
                Where a.DepartmentID = @DepartmentID and c.IsDeleted = 0 and b.IsDeleted = 0 and f.PhoneTypeID = 1;";
    ...

    ...
}

当它执行时我得到一个错误

执行超时已过期。在操作完成之前超时时间已过或服务器没有响应。

结果应该只返回 45 行,所以它不是很大。当我在 SQL Server Management Studio 中运行查询时,这不是问题,我得到了 45 行。

但是在我的应用程序中,如果我在声明中加上一个顶部,那么它就可以工作:

var sql = @"select top 1000 *
            From Contacts.Departments a
            Left Outer Join Contacts.ContactMapping b on a.DepartmentID = b.DepartmentID
            Left Outer Join Contacts.Contacts c on b.ContactID = c.ContactID
            Left Outer Join Contacts.Divisions d on b.DivisionID = d.DivisionID
            Left Outer Join Contacts.Emails e on b.EmailID = e.EmailID
            Left Outer Join Contacts.Phones f on b.PhoneID = f.PhoneID
            Where a.DepartmentID = @DepartmentID and c.IsDeleted = 0 and b.IsDeleted = 0 and f.PhoneTypeID = 1;";

或者如果我取出参数化查询并直接在字符串中添加值:

var sql = $@"select *
            From Contacts.Departments a
            Left Outer Join Contacts.ContactMapping b on a.DepartmentID = b.DepartmentID
            Left Outer Join Contacts.Contacts c on b.ContactID = c.ContactID
            Left Outer Join Contacts.Divisions d on b.DivisionID = d.DivisionID
            Left Outer Join Contacts.Emails e on b.EmailID = e.EmailID
            Left Outer Join Contacts.Phones f on b.PhoneID = f.PhoneID
            Where a.DepartmentID = {department_id} and c.IsDeleted = 0 and b.IsDeleted = 0 and f.PhoneTypeID = 1;";

这太奇怪了,因为我没有接触过代码,突然它就失败了。当我在我的开发机器上进行测试时,我可以重新创建上面的错误和解决方案。

我对要检查的内容有点迷茫,因为当我将查询修改为上述两者之一时,查询才有效。但是,那么为什么在尝试从 SQL Server 2016 检索数据时原始文件会失败

标签: sqlasp.netsql-serverado.net

解决方案


检查您的连接字符串超时变量

例子

集成安全 = SSPI;连接超时 = 30


推荐阅读