526

I get the following error when trying to connect to SQL Server:

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)

This error is thrown when I try to configure my database to gridview in Visual Studio 2010. I'm at a loss as to how to debug this error.

How would you debug this error? What steps should I take in order to determine what is really going on here, in addition to the one mentioned in the error message?

15
  • 14
    A ping is unreliable for testing SQL Server connectivity, ICMP echo request are disabled by default in Windows Server. An invalid username or password is not what the error is telling you at all, that's a completely different error.
    – Sean Airey
    Aug 5, 2013 at 14:32
  • 2
    Try this article, it goes through pretty much all the steps you will need to troubleshoot your connectivity problems: social.technet.microsoft.com/wiki/contents/articles/…
    – Sean Airey
    Aug 5, 2013 at 14:33
  • 4
    See my answer here with my screen shot if you received this error with SQL Server {version} Express, when setting it up for the first time. I came back to StackOverflow and used my answer again because it was the only one that worked. stackoverflow.com/questions/1391503/… Jan 21, 2014 at 3:47
  • I had this Issue on my virtual Server when I wanted to connect to the localhost. It appeared there was some kind of error when launching the OS - in my case everything was solved fortunately with a clean reboot.
    – Qohelet
    Feb 13, 2015 at 7:33
  • 2
    I had the same error. In my case I had not spelled the name of the server correctly. Sometimes it's the simple things.
    – Tarzan
    Mar 31, 2021 at 19:00

58 Answers 58

1
2
3

Along with trying everything as suggested by Teo Chuen Wei Bryan, make sure you are also referring to the correct Server/Instance name in the connection string.

If you are using the short form of host name/Instance on the database server or in the web.config file, make sure you use the fully qualified domain name(FQDN)/Instance

Also, to test connectivity from a server where SQL server client is NOT present,

--> create a text file and change its file extension to .udl

--> Right click the file and you can see connection tab.

--> Input server name and log on information to test connection to the database server.

Hope this helps.

3

If you suddenly encounter this error say in a production environment and nothing has changed, try the following 4 items in the order below to see if it gets fixed.

  1. restart the sql server service.
  2. restart the service (say IIS) that is calling into sql server. (the problem is probably here if the time between the start of the service call to SQL server and the time you end up getting the response error is super short (about one or two second).
  3. restart the server sql server is on.
  4. restart the server the calling service is on.
3

Xml tag arrangement in Web.config is important

First

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>

After

<connectionStrings>
  <add name="SqlConnectionString" connectionString="Data Source=.; Initial Catalog=TestDB; Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
2

This error occurs when your sql server instance is stopped.

GO to all Programs>SQL Server >Configuration tools>SQL SERVER CONFIGURATION MANAGER

then click on SQL sERVER SERVICES, list of instances will appear, select instance in question and click on play icon on top tool bar, hope this will help.

this answer is very late(but better late than never;)

0
2

I resolved this problem by setting the project that makes use of Entity Framework as the start-up project and then run the "update-database" command.

2

I also faced to this kind of problem even if I use the correct credentials. Please make sure to execute the following actions.

Make sure your credentials for the sql server. In sql server field you have to use the computer name. enter image description here

To find the computer name, search on your computer as computer name, then you can find the computer name. Then paste it and try to connect.

If it is not working, try to start the sql server on your computer from services. Below screenshot will be helpful.

enter image description here

1
  • An update to my machine by my IT department changed my Computer's name. Nobody told me this was going to happen. I spent way too long trying to find a much more complex reason for my not being able to access my own SQL Server Express server only to find out that the SERVER which is my local PC was changed without my knowledge. Sep 8, 2023 at 20:09
1

It seems that your instance of localdb is not running. To start it on computer startup, add into your Start menu \ Startup folder BAT file with following line

sqllocaldb start name_of_instance

where name_of_instance is name of localdb instance, which you want to start. You can list available instances in command line using sqllocaldb i.

e.g. If you're using SQL Server Management Studio and connecting to Server Name (localdb)\v11.0, then your BAT file will look like

sqllocaldb start v11.0
1

My issue was that you need to have a connection string entry in both your repository layer and web layer. Once I added it to my web.config as well as my app.config, Entity Framework was able to create the migration.

My question is why, does the web.config need it, when there is absolutely no database access there.

1

For me, the solution was to call the internet provider to open port 1433.

For some reason, they were blocking the port, and I couldn't even ping the database server (hosted on Azure). 100% of the packets were lost.

After they opened the port everything worked smoothly again.

1

I got Solution for me when trying to access mssql server from other network :

Open "SQL Server Configuration Manager"

Now Click on "SQL Server Network Configuration" and Click on "Protocols for Name"

Right Click on "TCP/IP" (make sure it is Enabled) Click on Properties

Now Select "IP Addresses" Tab -and- Go to the last entry "IP All"

Enter "TCP Port" 1433

enter image description here

Add inbound+outbound rule for port 1433

1

I had the same issue after upgrading my Windows to Windows Pro 10 21H2. I have two SQL instances - 2014 and 2019 and I was not able to connect only to 2019 one. I restarted the computer, restarted all SQL related services. Two of the services were showing errors (SQL Server and SQL Server agent) and I was not able to start them. I could not also start these two services from C:\Windows\SysWOW64\SQLServerManager15.msc In the logs (C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER01\MSSQL\Log) I saw these lines:

Initializing the FallBack certificate failed with error code: 15, state: 29, error number: 0.
Error: 17190, Severity: 16, State: 1.
Unable to initialize SSL encryption because a valid certificate could not be found, and it is not possible to create a self-signed certificate.
Error: 17182, Severity: 16, State: 1.
TDSSNIClient initialization failed with error 0x80092004, status code 0x80. Reason: Unable to initialize SSL support. Cannot find object or property. 
Error: 17182, Severity: 16, State: 1.
TDSSNIClient initialization failed with error 0x80092004, status code 0x1. Reason: Initialization failed with an infrastructure error. Check for previous errors. Cannot find object or property. 
Error: 17826, Severity: 18, State: 3.
Could not start the network library because of an internal error in the network library. To determine the cause, review the errors immediately preceding this one in the error log.
Error: 17120, Severity: 16, State: 1.
SQL Server could not spawn FRunCommunicationsManager thread. Check the SQL Server error log and the operating system error log for information about possible related problems.

It turned out that after the upgrade the "Log On" property (second tab on service properties) of the service was set to NT AUTHORITY\NETWORK SERVICES. When I changed it to "Local System Account" I was able to start the service and connect to the MS SQL 2019 instance.

This msdn forum post has helped me to find this.

1

If you are removing migrations, you should check your ConnectionString.

1

Faced the similar problem. For me, the appsettings.Production.json file was not being used by production server and the application was using appsettings.json file. Environment Variable in the system was not set to Production. It was directing requests to a different server overall.

1

If Tomcat server or any server you are using is down then this error might occur.

1

I got this error after working at distance (VPN) for a few years. At some point in time I must have changed my DNS settings to the IP address of my home router. Now I was (again) in the same network as the server, and got the error above. the solution was to open "Network status" / "Change adapter options" and right click the active internet connection (Ethernet in my case) to get to the properties of IPv4. Under "Use the following DNS server addresses" I entered the local IP address of my server, and what do you know...! The computer contacted the domain (on the server) and SQL Server opened up.

To check if you have the same problem, open cmd and enter nltest /dsgetdc:domainname [exchange "domainname" with the name of your domain] If you get a message saying "Getting DC name failed: Status = 1355 0x54b ERROR_NO_SUCH_DOMAIN" you either got the domain name wrong, or you may have this problem.

1

after tried all answers, i put in port number(1433) after servername with comma into connection string worked for me. Its in below :

"Server=**.***.***.*\\MYSERVER,1433;Database=****;User Id=****;Password=*****;MultipleActiveResultSets=true;TrustServerCertificate=True"
1

FOR AZURE VM USERS

I have tried all of the above (so those still might be valid) but still didn't wotk.

IMPORTANT: Mine was a just a playground server so security was really not a concern. So do your research, this is just a workaround, and not a prod-safe approach.

Make sure you can ping your server. In my case, I was able to RDP but cannot ping my server on Azure. I was keep getting Request timed out. Beacuse seems Azure blocks ICMP protocol by default.

You can add a rule to firewall. In my case, regardless what I have added through the firewall on that machine, nothing changed. Thanks @John for the above answer, that reminded me too that I have to add this from the Networking on Azure Portal.

As soon as I added a rule for port 0, then finally I was able to connect remotely.

this link this link was also helpful.

enter image description here

1

I ran on a similar problem while using MS SQL 2022. When initially installed I was able to access the database using MSSQL Management Studio, but after the computer restart the SQL Server engine won't start automatically. In my case it was the account used to logon, the default account is MSSQLSERVER. I changed the account to my computer account (or domain account, if you are on a corporate environment)... on the Computer Management >> Services and Applications >> SQL Server >> right click >> Properties >> Log On >> This account >> enter userid password and confirm password. Then click the start button to start database engine.

1

Windows Server 8 R2 and SQL Server 8 R2

I Just Restared my Server PC And Issue resolved.

1

My issues was that I was using localhost/12.7.0.0.1 to connect but it was disabled in Sql Server Configurations.

Open Server Confuguration Manager -> SQL Server Network Configuration -> Protocols for MSSQLSERVER -> TCP/IP (Make sure this is enabled) ->Properties -> Enable

You will need to restart the server for changes to take effect. enter image description here

0

It could also be as simple as the fact that your database is not actually MS SQL Server. If your database is actually MySql, for instance, and you try to connect to it with System.Data.SqlClient you will get this error.

By far most examples of ADO.Net will be for MSSQL and the inexperienced user may not know that you can't use SqlConnection, SqlCommand, etc., with MySql.

While all ADO.Net data providers conform to the same interface, you have to use the provider made for your database.

0

I also had this issue, I tried everything which suggested in this post but nothing worked. Finally I solved my issue by executing this query in my SQL Server

EXEC sp_configure 'remote access', 0 ;  
GO  
RECONFIGURE ;  
GO  
0

I know this is buried, but I had caused this issue when I moved a database without giving the NT Service\MSQLSERVER account rights to the directory I moved the database files to. Giving the rights and restarting SQLServer did the trick.

0

Verify the account you are using to create a session for the SQL Server instance.

See

0

If SQL Server is running and you still get an error; When SQL Server is opened, instead of the default server name which is displayed you should select the server name ending with \SQLEXPRESS under the Server name-> Browse for more-> Database engine option.

0

In my case, nothing from above worked, because ESET security had its own layer of firewall, and only after I disabled that, the things started working (and after doing the things that other suggested).

-1

I have tried almost everything. Then, I have uninstalled Malwarebytes. A software which claims that it can remove malware (often it can). After that, all is fine. Maybe somebody might run into the same issue for the same reason.

1
  • 1
    Very unlikely that this is related. Aug 26, 2021 at 8:44
-1

I came here so late with the same problem. For me the problem came coz I changed the pc name.

1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.