
.NET跨平台之旅:数据库连接字符串写法引发的问题
发布日期:2021-05-09 01:35:20
浏览次数:18
分类:博客文章
本文共 1825 字,大约阅读时间需要 6 分钟。
最近在一个ASP.NET Core站点中遇到一个奇怪问题。当用dotnet run命令启动站点后,开始的一段时间请求执行速度超慢,有时要超过20秒,有时甚至超过1分钟,日志中会记录这样的错误:
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
当这些执行超慢的请求执行完成后(只是超慢,但没有出现500错误),一切就恢复正常,上面的错误日志也不再出现。
更奇怪的是这个问题只在Linux服务器上出现,在Windows服务器上不会出现。
在园子里看到“”博文后,我们怀疑可能是SQL Server数据库服务器没有安装SP3(我们用的是阿里云RDS)。
这个问题,表象上体现的是一个连接超时的错误:
Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught) ---> System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address但是这个错误却是因为SQL Server的版本问题造成的,.NET Core中的SqlClient能支持的SQL Server最小版本为 SQL Server 2008 R2 SP3,如果你的数据库小于这个版本,就会出现这个异常。官方的Issues在此:
今天晚上突然想到,会不会是数据库连接字符串写法的问题,换一种写法试试。于是将connection string由
"Data Source=server-name;initial Catalog=database-name;user=username;password=password"
改为
"Server=server-name;Database=database-name;user=username;password=password"
之后,问题就神奇地解决了!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月17日 09时45分21秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
可变长度参数
2019-03-11
cordova打包apk更改图标
2019-03-11
GitHub上传时,项目在已有文档时直接push出现错误解决方案
2019-03-11
文件系统的层次结构
2019-03-11
vue(渐进式前端框架)
2021-05-14
vscode设置eslint保存文件时自动修复eslint错误
2021-05-14
Remove Extra one 维护前缀最大最小值
2021-05-14
Linux操作系统的安装与使用
2021-05-15
C++ 继承 详解
2021-05-15
OSPF多区域
2021-05-15
Docker入门之-镜像(二)
2021-05-15
重置UAG Application admin密码
2021-05-15
嵌入式系统试题库(CSU)
2021-05-15
setup facatory9.0打包详细教程(含静默安装和卸载)
2021-05-15
Linux kernel pwn --- CSAW2015 StringIPC
2021-05-15
IDEA 找不到 Persistence窗口解决办法
2021-05-15
Form窗体属性
2019-03-12
vue 错误收集
2019-03-12
00010.02最基础客户信息管理软件(意义类的小项目,练习基础,不涉及数据库)
2019-03-12