sql - 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 检索数据时原始文件会失败
解决方案
检查您的连接字符串超时变量
例子
集成安全 = SSPI;连接超时 = 30
推荐阅读
- javascript - 如果我没有域,如何将 CORS 标头添加到我的 REST API?
- sql - SQL - 创建一个返回值对的查询
- xcode - SwiftUI:如何每天刷新 Apple Watch 复杂功能
- flutter - Dart 对从概念上改变属性的操作使用 setter
- html - 有没有办法去另一个页面,同时去那个页面中间的一个id
- azure - Azure - 在 Powershell 中断开 VNet 集成
- javascript - 是否可以从 pug 中的 onclick 事件中调用 mixin?
- python - 如何绘制方程的实部?
- firebase - android vs firebase函数中的Firestore时间戳格式
- php - 文件意外结束,期待 elseif (T_ELSEIF) 或 else (T_ELSE) 或 endif (T_ENDIF)?