How do I fix the error 'Named Pipes Provider, error 40 - Could not open a connection to' SQL Server'?

前端 未结 30 2539
借酒劲吻你
借酒劲吻你 2020-11-22 08:27

I can\'t seem to connect to my database from a site. I get this error:

Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

相关标签:
30条回答
  • 2020-11-22 09:10

    Solving this problem is very easy:

    1. Go to control panel.
    2. search for services.
    3. Open Local services window from your search results
    4. Restart your MSSQLSERVER service.

    Screenshot of the steps:

    Screenshot of the steps

    0 讨论(0)
  • 2020-11-22 09:10

    You will find most likely your DB name is not correct, you will see the server name in VS like "DESKTOP-0I14BKI" but if you open up SSMS you will see DESKTOP-0I14BKI\SQLBLAHBLAH , simply add "\SQLBLAHBLAH" (instance name) to your "server name" in VS connection properties.

    You will see:

    To Fix:

    0 讨论(0)
  • 2020-11-22 09:12

    I had the same problem. I use the MSSQL Server Management Studio 2017 and solved this problem using these steps:

    1. Check for working fine SQL Server Services services or not.
    2. Also check for working in good condition SQL Server (MSSQLSERVER).
    3. Also check for working fine SQL Server Browser.
    4. Restart SQL Server (MSSQLSERVER)

    and fixed it.

    0 讨论(0)
  • 2020-11-22 09:12

    TL;DR; Your SQL Server instance is using dynamic ports which is not working. Force SQL Server to use static port # 1433.

    Complete Details: First of all this problem is more likely if you've a mix of default and named instance or named instances only(which was my case).

    Key concept: Each instance of Microsoft SQL Server installed on a computer uses a different port to listen for incoming connection requests. Default instance of SQL Server uses port # 1433. As you install named instances then they will start using dynamic ports which is decided at the time of start-up of Windows service corresponding to named SQL Server instance.

    My code was failing (with error code 40) to connect to the only named SQL Server instance that I had on my VM. You can try below possible solutions:

    Solution # 1: Client code trying to connect to SQL Server instance takes help from SQL Server browser service to figure out port number at which your named instance is listening for incoming connections. Make sure SQL browser service is running on your computer.

    Solution # 2: Check the port # (in yellow color) your named SQL Server instance is using from SQL Server configuration manager as shown in the snapshot below:

    Use that port number explicitly in your connection string or with sqlcmd shown below:

    sqlcmd -s mymachinename,11380 -i deleteDB.sql -o SQLDelete.txt
    

    Solution # 3: Force your named instance to use port # 1433 which is used by default instance. Remember this will work only if you don't have any default SQL Server instance on your computer as the default SQL Server instance would be using using port # 1433 already. Same port number can't be uses by two different Windows services.

    Mark TCP Dynamic ports field to blank and TCP Port field to 1433.

    Change the port number in your connection string as shown below:

    sqlcmd -s mymachinename\instanceName -i deleteDB.sql -o SQLDelete.txt
    

    OR

    sqlcmd -s mymachinename,1433 -i deleteDB.sql -o SQLDelete.txt
    

    Note: Every change in TCP/IP settings requires corresponding Windows service restart.

    Interestingly enough after resolving the error when I went back to dynamic port setting to reproduce the same error then it didn't happen. Not sure why.

    Please read below interesting threads to know more about dynamic ports of SQL Server:

    How to configure SQL Server Port on multiple instances?

    When is a Dynamic Port “dynamic”?

    When to use a TCP dynamic port and when TCP Port?

    I got leads to solution of my problem from this blog.

    0 讨论(0)
  • 2020-11-22 09:13

    Did have the same problem. Spent like 6 hours when had to migrate some servers. Tried all suggestions available on this topic and others.

    Solution was as simple as server restart!

    0 讨论(0)
  • 2020-11-22 09:13

    Try the following steps:

    1. Open Services window (open "run box" and type services.msc).

    2. Looking for SQL services (with SQL prefix).

    3. Start them (if cannot start. Goto step 4).

    4. Right_click to each service -> Properties -> Change to tab "Log on"-> choise log on as "Local ..." -> 0K. Then start SQL services again.

    Try Open SQL and connect database.

    0 讨论(0)
提交回复
热议问题