MSSQL - A network-related or instance-specific error occurred

I have witnessed this error quite a few times in my career. I see it every time there is something wrong with the network in a datacenter, previously this had been in a private cloud but last week I saw it on a public cloud (Azure). The error looks something like the following:

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

I assume this is not a new connection to the server. If it is, the issue can be anywhere from the server being offline, to you using the wrong connectionstring, to not being able to reach the server due to a firewall. There are a lot of good suggestions of what to look for here.

Often applications are on another network than the database is. So changes to the network can cause this error if the applications can no longer reach the database. You might even see some applications being able to reach the database and others not. I have seen this with both kubernetes nodes and services on windows servers. If the error is sudden, you need to start isolating and troubleshooting it:

  • Check that the SQL server is running. Check if you can connect to the server, you might want to try this from a couple of different places, such as your work station, the server on which the application is running and perhaps check to see if you can connect from the server that the MSSQL is on. The last two might not be possible depending on your environment. Overall you want to make sure that the MSSQL is up and running.
  • Check If any applications can connect to the database. If you have applications in different datacenters/locations you might want to check if some of them can connect. Networks today can be rather complex, so it is good to try and isolate the issue. Microsoft has a great network troubleshooting guide here.
  • Check for changes in the application. Figure out when the issue started and if any new code was deployed around that time. If there was a code change the error could reside there. Just note that this is not a syntax error, the error is that the application cannot reach the MSSQL server.

The above is what I would do. However I have a tendency to start with the last thing as I blame my own changes rather than the datacenter. I think the right approach is starting from the top and verifying the connection, isolating the issue is the most important!

I hope you found this useful, let me know what you think down in the comments below!