Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

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: Shared Memory Provider, error: 40 – Could not open a connection to SQL Server) (.Net SqlClient Data Provider)

Posted by ken zheng on March 21, 2008

When connecting to an instance of Microsoft SQL Server 2005 database, including SQL Server 2005 Express, Developer and Enterprise editions, for authentication or accessing and retrieving and updating the data, the the following error message may appear. The problem happens no matter the connection is originated from remote computer or on SQL Server local computer itself, and can be caused by any program, such as SQLCMD utility, used to connect to SQL Server.

Sqlcmd: Error: Microsoft SQL Native Client: 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.

Or,

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) (Microsoft SQL Server, Error: 2)

This problem may occur when SQL Server 2005 is not configured to accept incoming local and remote connections, which is disabled by default in SQL Server 2005 Express Edition, SQL Server 2005 Developer Edition and also SQL Server 2005 Enterprise Edition. To solve the connection forbidden problem, SQL Server 2005 needs to configure to allow incoming local and remote connections.

Firstly, ensure that SQL Server 2005 is configured properly to allow incoming connections on the instance of database server, else enable and turn on the local and remote connections setting.

Click Start button, then go to Programs or All Programs, then select Microsoft SQL Server 2005, followed by Configuration Tools. Click and run the SQL Server Surface Area Configuration.
On the “SQL Server 2005 Surface Area Configuration” page, click Surface Area Configuration for Services and Connections.
On the “Surface Area Configuration for Services and Connections” page, expand Database Engine tree, click Remote Connections.
Select Local and remote connections, or Local connections only which applicable only if there is no remote system tries to connect to the SQL Server, useful when you just trying to connect and authenticate with the server after installing.
Select the appropriate protocol to enable to local and/or remote connections environment. To ensure maximum compatibility, select Using both TCP/IP and named pipes.
Click Apply button when done.
Click OK button when prompted with the message saying that “Changes to Connection Settings will not take effect until you restart the Database Engine service.”
On the “Surface Area Configuration for Services and Connections” page, expand Database Engine, then click Service.
Click Stop button to stop the SQL Server service.
Wait until the MSSQLSERVER service stops, and then click Start button to restart the MSSQLSERVER service.
Secondly, SQL Server Browser service has to be enabled to allow for local and remote connections if SQL Server 2005 is running by using an instance name and users are not using a specific TCP/IP port number in the connection string.

Click Start button, then go to Programs or All Programs, then select Microsoft SQL Server 2005, followed by Configuration Tools. Click and run the SQL Server Surface Area Configuration.
On the “SQL Server 2005 Surface Area Configuration” page, click Surface Area Configuration for Services and Connections.
On the “Surface Area Configuration for Services and Connections” page, click SQL Server Browser.
Select Automatic as the Startup type to start SQL Server Browser service automatically every time system starts.
Click Apply button.
Click on Start button to start the service immediately.
Click OK button.
Finally, if remote computer needs to connect and access SQL Server, an exceptions in Windows Firewall included in Windows XP SP2 (Service Pack 2), Windows Server 2003 and Windows Vista needs to be created. If you’re using third-party firewall system, the exception rules also needed to be created to allow external remote connections to the SQL Server 2005 and SQL Server Browser Service to communicate through the firewall, else connections will be blocked. Consult the firewall manual for more details. Each instance of SQL Server 2005 must have its own exception, together with an exclusion for SQL Server Browser service.

SQL Server 2005 uses an instance ID as part of the path when you install its program files. To create an exception for each instance of SQL Server, you must identify the correct instance ID. To obtain an instance ID, follow these steps:

Click Start button, then go to Programs or All Programs, then select Microsoft SQL Server 2005, followed by Configuration Tools. Click and run the SQL Server Configuration Manager.
In “SQL Server Configuration Manager”, click the SQL Server Browser service in the right pane, right-click the instance name in the main window, and then click Properties.
On the “SQL Server Browser Properties” page, click the Advanced tab, locate the instance ID in the property list.
Click OK button.
Then create an exception for SQL Server 2005 in Windows Firewall.

Click on Start button, the click on Run and type firewall.cpl, and then click OK. For Windows Vista, type firewall.cpl in Start Search box and press Enter key, then click on Allow a program through Windows Firewall link on left tasks pane.
In “Windows Firewall”, click the Exceptions tab, and then click Add Program.
In the “Add a Program” window, click Browse button.
Click the C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe executable program, click Open button. MSSQL.1 with is a placeholder for the instance ID that is obtained from previous procedure. Note that the path may be different depending on where SQL Server 2005 is installed.
Click OK button.
Repeat steps 1 through 5 for each instance of SQL Server 2005 that needs an exception.
For SQL Server Browser service, locate the C:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe executable program, and click Open button.
Click OK button.

40 Responses to “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: Shared Memory Provider, error: 40 – Could not open a connection to SQL Server) (.Net SqlClient Data Provider)”

  1. Basel said

    I was having SQL 2000 then I formatted my PC and then I Installed SQL 2005, and SQL 2000 then I uninstall them both, after than I installed only SQL 2000. when I tried to open a connection to this SQL server from my .NET code (which was working fine previoulsy before teh formatting) the below message appears.

    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: Shared Memory Provider, error: 40 – Could not open a connection to SQL Server) (.Net SqlClient Data Provider)

  2. liedong said

    Does your code look for SQL 2005 or SQL 2000? You said you only SQL 2000 on your machine

  3. Antoinette said

    *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: Shared Memory Provider, error: 40 – Could not open a connection to SQL Server) (.Net SqlClient Data Provider)*

    This error also appeared when I’m trying to run my program in c#. I already tried the solution above but I still have this message. Is there any other solution?

    by the way, I’m using SQL 2005 express edition…

  4. liedong said

    Have you start the SQL Server? Open SQL Management Studio, and make sure the service is running.

  5. Itachi21 said

    I did everything and everything is running, still dont work…

  6. satheesh said

    thk u very much …. sql work’s perfectly

  7. Rob said

    The problem for me was that I had done several installs leaving at least 6 instances under C:\Program Files (x86)\Microsoft SQL Server i.e. MSSQL.1, MSSQL.2 etc. MSSQL.1 was the express edition and I had its service off. Once I turned the SQL Express service on the app worked.

  8. Rob said

    !!! The problem for me was that I had done several installs leaving at least 6 instances under C:\Program Files (x86)\Microsoft SQL Server i.e. MSSQL.1, MSSQL.2 etc. MSSQL.1 was the express edition and I had its service off. Once I turned the SQL Express service on the app worked.

  9. Venkatesan said

    Thanks ever somuch The problem is solved

  10. Ruchi said

    I m getting the same error and m unable to connect with database engine..but can do with integration services. With server type ‘Database Engine’ it doesn’t give me connect option.
    Moreover, there is some problem in installation as reporting services cant be installed…

  11. Dachi said

    Hey I followed this and did the steps closely, but the same error message still shows up. Is there another way to deal with this problem that i am missing?

  12. merope said

    this helped me a lot!!
    thank you soo much :)

  13. sonu said

    Try restarting MS SQL Server after checking the above settings to allow remote connections

  14. jagan said

    see here u wrote we need to stop and start the service in middile .but Iam getting error message like An error has occured while performing the operation .here what might be the reason?

  15. ZiKaS said

    Thank you so much

  16. Eyitayo said

    Thanks sooooooooo much.
    I had almost lost my mind while changing my SQL server settings before i got to this site.
    This is very wonderful , your the best!!!

  17. Tim said

    This article helped me a lot….Thank you…

  18. Monique said

    Hi there

    I’ve tried this and I’m still getting the error.

    Please help. I’m using SQL 2005 Express Edition

  19. Monique said

    Ok, nevermind, my connection string was incorrect was being fusy.

    problem solved

  20. its helpful, thanx ton..

  21. I appreciate this forum, cauz many of my runtime issues has been solved by this.. dont have time else i would have done more this site..

  22. I would be better if v all answer pointwise, for eg.,
    if need for installation, will write steps one by one,

    Thanx,

  23. Mihir said

    I hav installed SQL Server 2005 on my PC (XP SP2).
    Earlier i had no problem in connecting to the server but suddenly
    i hav started getting this error when i try to connect.

    “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) (Microsoft SQL Server, Error: 2)”

    i tried everything that u hav mentioned above but my MSSQLSERVER service does not start;neither manually nor automatically.
    i hav selected the local connection only option.

    My Named pipes ,tcp/ip and other protocolas r also enabled.

    Please help
    Thanks.
    MIHIR

  24. pepsigrl said

    yar seriously dis help me alot i mean ma sql was suddenly giving di s mesage n m shocked couz m final year student and after 3 days ma endtdate is comming so ms hocked and very woorried couz i thought maybe system need to window reinstall and uffffffff dis is so much time consuming suddely idea came on ma mind i searches on google and find dis fabolous site and look ma prob is resolved
    so thanku so much

  25. A.Salam said

    Hi,

    Please ensure that the server name you are giving should be like \SQLEXPRESS

  26. Sajid said

    Hello

    Am using SQL SERVER 2000 and am unable to connect to sql server.
    the same error appear saying Sql server does not allow remote connections……………………………..

    Where am I suppose to change my sql server settings.

    Many thanks in advance.

    regards
    Sajid

  27. deepak said

    thanx for solving my problem

  28. ritesh said

    {”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)”}

  29. Vision said

    Dear,

    I had created a Test.mdf Databse in App_Data folder using Visual Web Developer 2008 Express Edition. When i tried to connect to Test.mdf DB with my StringConnection as:”Data Source=~/SQLEXPRESS;AttachDbFilename=D:/MYPROJECT/WEBSITE/LoginStatusControl/App_Data/Test.mdf;Integrated Security=True;User Instance=True”,
    then i start debugging my Website, i logged in (using Login Control in WVD) i got the follow error:
    ” {”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)”} ”

    I hope to recieve your advice via email (trunguc1089@gmail.com)
    Thanks very much for any help.!
    Trunguc.

  30. Fawzy said

    Thanks for your nice article

  31. Ramya Sharon said

    Hello..
    I am Ramya Sharon.. I have tried to initialize tcp/ip and named pipes protocol according to your advise on the error specified here..
    However, i get the following error.. though i am an administrator.. what do i do…?? please help..!!

    “” The service did not respond to the start or control request in a timely fashion, you need administrator privileges to be able to start/stop this service. (SQLSAC)”"

  32. Mamoun J. said

    Thanks Alot, This Article was very useful.

  33. neha said

    hieee

    i have tried everything but its not working at all.please help me soon as my work is pending because of that..
    reply me sooooon

  34. Actually my problem is not very straight forward. I am trying to transfer data from one database to another using INtegration service and on a scheduled job. But whenever I am trying to run it, I am getting this.. No idea how to resolve it… Now trying a trial and error method… No luck yet

  35. rohit kandhal said

    hi all …
    me too getting same error. i’ve tried everything posted above.
    i’ve enabled sql server authentication, tell me about the connection string too..
    conPubs = new SqlConnection(@”server=localhost;Integrated Security=SSPI;database=rohit”); is it correct ??

    thx.

  36. Yasin said

    Great post!!!
    Thanks a lot :)
    Cheers,
    Yasin

  37. Rizwan said

    i have tried all above mention but still i m not access the 2005 DB in 2000

    but from 2005 i can access 2000 DB.

    its shows connection fail error.from sql server 2000.

    pls help me

  38. gagan said

    thnx allot

  39. student said

    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: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)

  40. student said

    sql server 2005 express ran well for the first time with visual studio but after tht it gave this problem…plz help….

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>