SqlClient.SqlException - An error has occurred while establishing a connection to the server
|
|
|
|
|
If you are new to using SqlServer 2005 and did default installation of the server, you will very quickly run into this exception the momet you try to
connect to the database server from a remote machine using ADO.Net.
Server Error in '/GridViewDemo' Application.
--------------------------------------------------------------------------------
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005,
this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated
in the code.
Exception Details: System.Data.SqlClient.SqlException:
An error has occurred while establishing a connection to the server.
When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings
SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
This exception gets thrown because default installation of SQL Server 2005 (Express and Developer Edition does not allow remote connections
to the server. The default settings is to allow local connection only. This works well where every developer has their own installation of SQL
Server on local machine and doing development againt it. But if your connecting to remote database server then you are out of luck till the
configuration is modified to allow connections from remote locations.
SQL Server 2005 has a new configuration tool "SQL Server Surface Area Configuration". The name pretty much suggests that Microsoft has made
a decent attempt to let administrators to reduce attack surface area of SQL Server. I am sure you remeber dreaded "slammer" worm that
brought down tons of SQL Server. If your server is configured to allow local connections only, then this kind of problem would not have
started at first place. I experimented it with one of my production servers. I made the change to allow remote connections. Within couple of hours
I started seeing attacks from remote machines trying to gain access to the server. But lets down to our problem in hand. We have a SQL Server 2005
installation and we want to allow remote machines to connect to it via ADO.Net etc. Follow the following procedure to use Surface area configuration
tool to make the change in your server.
SQL Server Surface Area Configuration
1. From Start Menu launch the configuration tool.
<
2. You will come to this next dialog box from where you can click on "Surface Area Configuration For Services And Connection" link.
3. If you choose default installtion, your configuration for connections will look something like the following snapshot.
4. Click on Local and remote connections option. And then choose the protocol you would like to use for connection. After the change
your configuration looks like the following.
5. Now restart your database service and you should be all set to go.
|