SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: )

Regular readers of my blog are aware of the fact that I have written about this subject umpteen times earlier, and every time I have spoken about a new issue related to it. Few days ago, I had redone my local home network. I have LAN setup with wireless router connected with my four computers, two mobile devices, one printer and one VOIP solution. I had also formatted my primary computer and clean installed SQL Server 2008 into it. Yesterday, incidentally, I was sitting in my yard trying to connect SQL Server located in home office and suddenly I stumbled upon the following error. Finding the solution was the most infuriating part as it consumed my precious 10 minutes.

Let us look at few of the common errors received:

An error has occurred while establishing a connection to the server.

(provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 5)

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: 1326)

I eventually found the resolution to this error on this blog itself; so I was saved from hunting for the solution. I am sure there are a number of developers who had previously fixed this error while installing SQL Server 2008 or SQL Server 2005 but in due course forgot the right solution to this error. This is just a quick refresher. Make sure to bookmark this as you never know when you would need this solution.

Let us check into the steps to resolve this error.

1) SQL Server should be up and running.

Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> SQL Server Services, and check if SQL Server service status is “Running”.

In addition, ensure that your remote server is in the same network. Run “sqlcmd -L” in your command prompt to ascertain if your server is included in your network list. You can even find tutorial for the same here SQL SERVER – Find All Servers From Local Network – Using sqlcmd – Detect Installed SQL Server on Network.

I have confronted numerous situations when these nerve-wracking errors crop up, and most of the time when I try to troubleshoot I notice that SQL Server services are neither running nor installed. If SQL Server is not installed as default instance SQL Server Browser should be running together with it; we will explore this further in Topic 5.

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_7

2) Enable TCP/IP in SQL Server Configuration

When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.

Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_1

Right Click on TCP/IP >> Click on Enable

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_2

You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.

3) Open Port in Windows Firewall

Windows Firewall is very efficacious in protecting the Operating system from all sorts of malicious attacks. By default, many of the ports and services are refrained from running by firewall. Time and again, SQL Server ports are not open in firewall as well. All the ports on which SQL Server is running should be added to exception and firewall should filter all the traffic from those ports. As described, by default SQL Server runs on port 1433, but if default port is changed then the new port should be added to exception. If SQL Server has named instance (another instance besides default instance) is installed, SQL Server browser should also be added to the exception, as described in Step 7.

Go to Control Panel >> Windows Firewall >> Change Settings >> Exceptions  >> Add Port

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_3

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_4

Make the following entries in popup “Add a Port” and click OK.
Name : SQL
Port Number: 1433
Protocol: Select TCP

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_5

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_6

4) Enable Remote Connection

Enabling remote connection is another important, yet oft-neglected step that is frequently missed by database administrators while setting up SQL Server. If this feature is turned off SQL Server will function smoothly on local machine, but it will let another server connect to it remotely. By default this feature is ON in SQL Server 2008.

Right click on the server node and select Properties.

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_9

Go to Left Tab of Connections and check “Allow remote connections to this server”

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_10

5) Enable SQL Server Browser Service

If SQL Server is not installed as default instance but instead installed as named instance and also if there is no specific TCP/IP port configured, it will give rise to the error that is being discussed in this article. If SQL Server Browser service is enabled, it will allow the server to be connected through dynamic TCP/IP port. Enabling this service is a one-time process, as on enabling it once it will apply to all the instances installed on the same server.

Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> SQL Server Browser

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_8

Right Click on SQL Server Browser >> Click on Enable

6) Create exception of sqlbrowser.exe in Firewall

As elucidated in Step 6, sqlbrowser service needs to be enabled for named instance. Windows Firewall may prevent sqlbrowser.exe to execute. So, it is imperative to add exception for the same in windows firewall.

Search for sqlbrowser.exe on your local drive where SQL Server is installed. Copy the path of the sqlbrowser.exe like C:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe and create the exception of the file in Firewall, as delineated in Step 3.

7) Recreate Alias

It is getting quite common to create alias of SQL Server and use it in application. This will ensure that in future if any physical SQL Server has to be moved, it will not be required to change any code or connection string. You can simply create alias with the same name pointing to different SQL Server and it will start working instantaneously. I have observed that a couple of times due to internal error while recreating alias this error was fixed.

Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Native Client 10.0 Configuration >> Aliases

Delete the alias that is giving problem and recreate it with identical parameters.

SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: ) e40_11

I have tried my best to include all the methods of fixing this error and if I have missed any, please leave a comment and I will be very glad to include them here. I have put in my effort to encompass this issue in one article that needs to be refereed when any connection error comes up.

Watch SQL in Sixty Seconds video to Resolve Connection Error:

[youtube=http://www.youtube.com/watch?v=1k00z82u4OI]

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Error Messages, SQL Scripts, SQL Server Management Studio
Previous Post
SQL Server – Download PDF SQL Server Cheat Sheet
Next Post
SQLAuthority News – SQL Server Energy Event with Rushabh Mehta – May 20, 2009

Related Posts

658 Comments. Leave new

  • try to add System.Configuration reference in your class library project

    Reply
  • Sorry for my last post i mistaken not the references. if you try using web.config it you using asp.net project. Im also experience with same error when i try to connect my Sql server 2005 standard Edition.

    step 1.
    try to check Sql server if you are using this configuration Servername:192.168..\databaseschema
    username:xxx
    password:xxx

    step 2.
    {Server side sql server Standard Edition}

    locate Sql server Configuration Manager
    – Protocols for mssqlserver
    -TCP/IP ,Enabled
    -Name Pipes, Disabled

    Sql Native Client Configuration
    -TCP/IP , Enabled
    -NamePipes, Enabled

    step 3. Web.config {webservice project}

    ——————————————————————————————-
    { Possible cause of error’s }

    ——————————————————————————————-

    servername configuration server = 192.168.0.8;uid=…etc
    is defferent from server = 192.168..\databaseschema;uid= .. etc

    Correct me if i mistaken. thanks

    Reply
  • I just shared my experience on this error while I’m running webservice project.
    on the top is my alternative solution. I hope I can help some developers
    ———————————————————————————————–
    http://localhost:1595/Main/WebServiceTest.asmx?op=ServiceInsertTransact

    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
    at System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup)
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
    at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
    at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
    at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
    at System.Data.SqlClient.SqlConnection.Open()
    —————————————————————————————————

    Reply
  • step 3. Web.config {webservice project}

    Reply
  • mohammad seddiq
    February 4, 2012 11:28 am

    Thank you because of your good information about SQL Server. It solved my problem with sql server.

    Reply
  • TITLE: Connect to Server
    ——————————

    Cannot connect to DUALCORESQLEXPRESS.

    ——————————
    ADDITIONAL INFORMATION:

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

    For help, click:

    ——————————
    BUTTONS:

    OK
    ——————————
    can any buddy tell me what is proble

    Reply
  • Hi Pinal,

    I have a strange issue, I have installed SQL 2008 DE in of the cloud machine with a public IP address. I’m able to connect the SQL Data Base with “localhost” as well as machine name but not with IP address. I have enabled the TCP\IP as well as named pipes but no luck. If I disabled shared memory, it won’t connect with machine name or local host, not sure why. Any help on this

    Reply
  • got the following error while starting mssql-server service
    “The request failed or the service did not respond in a timely fashion. Consult the event log or other applicable error logs for details.”

    Reply
  • Darren Corbett
    March 4, 2012 1:14 am

    You are are a star my TFS installed caused this error followed the steps all good now….thanks a million

    Reply
  • hi all
    i am getting error as”TITLE: Connect to Server
    ——————————

    Cannot connect to COMPUTER_1.

    ——————————
    ADDITIONAL INFORMATION:

    An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

    ——————————

    A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 – The pipe has been ended.) (Microsoft SQL Server, Error: 109)

    For help, click:

    ——————————
    BUTTONS:

    OK
    ——————————
    ” how to solve this case help me.

    Reply
  • Nice and very helpful article. I thank for the author for wonderful article.

    Reply
  • Hi,
    I have worked out all the solutiins you have given in SQL express 2005. I am not able to connect remotely to databased placed on SQL server 2005 ( have Live IP address). In connection string i menton the IP address but falies to connect the database.

    Login failed, Preloginjandshake falied, such error are giving.

    Is there any limitation for SQL express 2005 to connect remptely that SQL express in not in Network but giving IP address it can not be connected?

    KIndly guide,

    Reply
  • Thanx a loott…..
    I have configure everything ecept Firewall.
    The article help me lot.

    regards
    Neel

    Reply
  • I have the following – Home Network – Setup
    Admin Logon – SQL Server 2005 – Windows 7 (32-bit) – LocalHost.

    Could NOT Log In via Windows Authentication Mode – ERROR 233. So Connected via SSMS as sa, used (Server) Properties, Security, and unwisely reset the Server authentication to Windows Authentication Mode.!

    Well stuck, so…
    Following bits of advice from other web users, I did this:
    (Windows 7:) Start – All Programs-Microsoft SQL Server 2005 – Configuration Tools – SQL Server 2005 Surface Area Configuration – Add New Administrator(2-keys icon 1st of 5 links) – Brings up “SQL Server 2005 User Provisioning Tool for Vista” – OK, OK You’re Windows 7, but this “Vista” tool still works (No Win 7 in SQL 2005 days, right?). At the top of this window, it says “Granting Administrative Rights to Windows Vista User: \”. Using the > arrow icon, select “Member of SQL Server SysAdmin Role on ” from “Available Privileges and > move it to the right-hand pane (“Privileges that will be granted to \”).

    Click OK.

    Now Log on to SSMS again, using Windows Authentication Mode – works for me!

    SQL Server 2005 is THE MOST AWKWARD Microsoft MONKEY, and takes HOURS off your life! Thankyou Microsoft!

    Reply
  • Thanks alot for the wonderful post, it saved my day :)

    Reply
  • Manikandan Sethuraju
    March 21, 2012 3:36 pm

    Great, this steps are very useful to me.
    it’s working fine..@@@@

    Thanks

    Reply
  • Thanks alot………………….:)

    Reply
  • You are a life saver, Why is it not mentioned anywhere in this tutorial. Mine had to with disabled TCP/IP. Knowledge is indeed power.

    Reply
  • I am getting this error
    “A network-related or instance-specific error occured while establishing 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) (Microsoft SQL Server, Error: 2)”

    I did all was said but I am not even able to log into my local server on my laptop. I have installed the SQL Server 2008 R2 on Windows 7.

    Reply
  • this is awesome. Thanks for your help
    -Swaps

    Reply

Leave a Reply