ASP.NET Core 7 MVC SQL connection error provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

6jygbczu  于 12个月前  发布在  .NET
关注(0)|答案(2)|浏览(128)

I moved my ASP.NET Core 7 MVC project from one computer to another. I did not make any changes in the code. I also moved and restored the database in the same way.

However, I cannot open my project and connect it to SQL Server. I get an error:
Win32Exception: The network path was not found.
Unknown location

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

I don't think there is a problem with the database because I was able to create a dummy console app and successfully connect to the database and retrieve the data.

What I tried

  • I checked the .net versions on both machines
  • I checked the version of the package, uninstalled and reinstalled. tried different connection strings

I think the problem is in the application because when I tried with the console app I connected successfully

This is my connection string:

"ConnectionStrings": {
    "db": "Server=localhost;Database=NIFBase;Trusted_Connection=True;"
}

Project packages: https://imgur.com/a/4qR0AQ6

tcomlyy6

tcomlyy61#

Provide what you have in SQL server management studio server name to the appsetting.json Server=SHADOW (In this example). Also make sure to add MultipleActiveResultSets=true at the endof your connection string.

{
"ConnectionStrings": {
 "DefaultConnection":"Server=localhost;Database=NIFBase;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}
kzmpq1sx

kzmpq1sx2#

you are facing connection issues

Create a Database user to access you database and use this connection string

"ConnectionString": "Server=[SQL_SERVER_INSTANCE];Initial Catalog=[DATABASE_NAME];Persist Security Info=False;
        User ID=[DATABASE_USER];Password=[PASSWORD];MultipleActiveResultSets=False;
        Encrypt=True;TrustServerCertificate=False;Connection Timeout=3600;",

相关问题