Why am I getting “Cannot Connect to Server - A net

2018-12-31 00:14发布

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

Cannot connect to 108.163.224.173.

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

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?

30条回答
与风俱净
2楼-- · 2018-12-31 01:05

Adding my heavily upvoted comment as an answer with screenshots.

I spent a lot of time on this, finally what worked for me is:

1) Open Sql Server Configuration Manager --> SQL Server Network configuration --> Protocols for <(INSTANCE)> --> TCP/IP (double click on it).

enter image description here

2) Select --> IP Addresses(Tab).

3) Go to the last entry IP All and mention TCP Port 1433.

enter image description here

4) Press Win+R and enter services.msc.

5) Now restart SQL Server <(INSTANCE)>.

enter image description here

After this, the problem got resolved!

查看更多
泛滥B
3楼-- · 2018-12-31 01:05

You may check service status of MS SQL Server 2014. In Windows 7 you can do that by:

  1. Go to search and Type "SQL Server 2014 Configuration Manager
  2. Then click on "SQL Server Service" on left menu
  3. Check the instance of SQL Server service status if it is stopped or running
  4. If it has stopped, please change the status to running and log in to SQL Server Management Studio 2014
查看更多
骚的不知所云
4楼-- · 2018-12-31 01:06

While the above solutions should work in 90% of the cases, but if you are still reading this answer!!! You are probably trying to connect to a different server than intended. It may be due to a configuration file pointing to a different SQL server than the actual server you think you are trying to connecting to.

Happened to me atleast.

查看更多
墨雨无痕
5楼-- · 2018-12-31 01:07

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.
查看更多
素衣白纱
6楼-- · 2018-12-31 01:08

My issue started when I tried to change the server from IIS Express to Local IIS (while using LocalDB).

enter image description here

I was using LocalDB (for dev purposes), and when I went to revert from Local IIS to IIS Express, Visual Studio had switched my data source from Data Source=(LocalDb)\MSSQLLocalDB to Data Source=.\SQLEXPRESS

Incorrect connection string

<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SurveyTestsDB.mdf;Initial Catalog=SurveyTestsDB;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />

Correct connection string

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SurveyTestsDB.mdf;Initial Catalog=SurveyTestsDB;Integrated Security=True" providerName="System.Data.SqlClient" />

Hope this helps someone out there.

查看更多
宁负流年不负卿
7楼-- · 2018-12-31 01:08

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.

查看更多
登录 后发表回答