我正在尝试手动连接到 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,但无济于事。
编辑
与此同时,微软 ADO.NET Core 团队的一名成员表示,该错误是基于.mdf
不存在的事实。
那么,建议使用什么模式让 ADO.NET 在指定位置创建数据库文件?
|