我正在尝试手动连接到 LocalDB(SQL Server 2019 和 SQL Server 2022 服务),使用 ADO.NET 创建新的应用程序数据库 (
Microsoft.Data.SqlClient
)。
每次尝试连接时,都会收到一个一般错误,告诉我无法附加数据库文件。
错误日志:
2024-09-17 19:30:18.40 Logon Error: 15350, Severity: 16, State: 1.
2024-09-17 19:30:18.40 Logon An attempt to attach an auto-named database for file C:\Temp\CvTest.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
2024-09-17 19:31:02.66 Logon Error: 15350, Severity: 16, State: 1.
2024-09-17 19:31:02.66 Logon An attempt to attach an auto-named database for file C:\Temp\CvTest.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
调试
Microsoft.Data.SqlClient
源代码,发现以下错误是问题背后的真正原因:
A connection to the server could be established, but an error occurred during the
login process. (provider: Named Pipes Provider, error: 0 - No process is at the
other end of the pipe).
导致此错误的原因是什么?
这是我的简单测试连接设置。这是一个廉价的连接字符串,每个 Microsoft 示例都引用它:
using SqlConnection con = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Integrated Security=SSPI;MultipleActiveResultSets=True;AttachDBFilename=C:\Temp\CvTest.mdf");
con.Open();
所以,它应该即兴发挥。但事实并非如此。
使用 SSMS,我可以轻松连接到 LocalDB 实例并立即创建/删除数据库,即使使用提供的路径也是如此。
我已经从MsLocalDb 15更新到MsLocalDb 16,但没有效果。