localdbsql-server-2022

Cannot connect to LocalDB (provider: Named Pipes Provider, error: 0 - No process is at the other end of the pipe)


I'm trying to manually connect to LocalDB (both, SQL Server 2019 and SQL Server 2022 services) creating a new application database using ADO.NET (Microsoft.Data.SqlClient).

Everytime I try to connect, I get a generic error telling me that the database file could not be attached.

Error.log:

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.

Debugging into the Microsoft.Data.SqlClient source code, the following error turned out to be the real reason behind the issue:

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).

What is causing this error?

This is my simple test connection setup. It's a cheap connection string that every Microsoft sample is quoting:

using SqlConnection con = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Integrated Security=SSPI;MultipleActiveResultSets=True;AttachDBFilename=C:\Temp\CvTest.mdf");

con.Open();

So, it should work off the cuff. But it doesn't.

Using SSMS, I can easily connect to the LocalDB instance and create/delete databases promptly, even with the provided path.

I already updated from MsLocalDb 15 to MsLocalDb 16, but to no avail.

EDIT

In the meantime, a member of the Microsoft ADO.NET Core team suggested that the error is based on the fact that the .mdf does not exit.

So, what is the suggested pattern to have ADO.NET create a database file at the specified location?


Solution

  • Instead of doing it using connection string, try using T-SQL. For example:

    enter image description here

    So get connected first, and after that attach the database using Transact-SQL.