Visual Studio emits a generic, verbose error message if it can’t connect to the SQL Server databse:
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)
The second sentence summarizes the issue: Visual Studio can’t connect to the SQL Server database. But why? Is it because the database is down? Is it a configuration error? What could it be?
Usually, this is caused by an incorrect or non-existent connection string. The first thing to do is check that you have a connection string defined. In
Web.Config, you should see:
If you don’t have a connection string, or if it’s commented out, then add one. Above is a sample that tries to connect to the local database.
Data Source=(local) is not a best practice. Actually, you should use (as our error message hinted) Named Pipes to connect. The equivalent connection string, with named pipes, would look like:
In this case, we’ve replaced
(local) with our SQL server instance name. We then need to double check that named pipes are enabled (they’re turned off by default in SQL Server).
To do that, load up the Sql Server Configuration Manager application. Under
Sql Native Client 10.0 Configuration (32 bit), browse to
Client Protocols, and set the
Named Pipes attribute to
That’s all it takes! Restart SQL server, double-check your connection string, and you should be ready to go!
And if things still don’t work? SQL Server makes it quite difficult to connect to the database and verify that things are working (at least, with the pre-packaged tools). I recommend you download Toad for SQL Server. It will detect and connect to the local server, as well as give you the fully-qualified connecting string (machine name and instance name).
If you can view the database, tables, and run queries, then at least you know that SQL Server is running. Otherwise, you’re walking in the dark.
To summarize: this error can be caused by any number of reasons. The core message is that Visual Studio can’t connect to SQL Server. To remedy it, check your connection string, and use Toad for SQL Server to hit the database and make sure that it’s up and running (and to generate the connection string with the server and database instance names).