当我尝试连接SQL Server时,我得到以下错误:

在建立与SQL Server的连接时,发生了与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。验证实例名称是否正确,SQL Server是否配置为允许远程连接。(提供商:Named Pipes提供商,错误:40 -无法打开到SQL Server的连接)

当我试图在Visual Studio 2010中将数据库配置为gridview时抛出此错误。我不知道如何调试这个错误。

如何调试此错误?除了错误消息中提到的步骤之外,我还应该采取哪些步骤来确定这里到底发生了什么?


当前回答

我遇到了同样的问题,问题是我在解决方案(Weband Droid)中有几个项目,即使在包管理器控制台中选择了默认项目,它也使用了Droid项目的连接字符串:

PM> update-database -Verbose
Using StartUp project 'Droid'. <-- DROID
Using NuGet project 'Web'. <-- WEB
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
<!-- BAD TARGET DATABASE -->
Target database is: 'DefaultConnection' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention).
System.Data.SqlClient.SqlException (0x80131904): 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
   [REMOVED TEXT]
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

在包管理器控制台设置启动项目到Web和默认项目后,我得到了它的工作。

其他回答

在做了上面提到的所有事情之后: http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/ 对我来说还是没用。

以下步骤对我很有效:

开始>执行> cmd > sqlcmd -L

它将提示您服务器名。确保此服务器名称与SQL management studio的CONNECT to server框中试图连接的服务器名称相同。

我犯了这个愚蠢的错误,我一直使用MSSQLSERVER而不是使用这个服务器名。

希望这对像我一样犯愚蠢错误的人有所帮助。

谢谢。

我通过打开服务来解决这个问题,然后开始运行Sql Server (Sqlexpress)服务。

我在更改电脑名称后遇到了这个问题。 所以我只需在SQL Server Management Studio中更新服务器名:

"服务器名称" -> "浏览更多…"-> "数据库引擎" ->选择"[您的计算机名称]" -> "OK"

您可以测试以下方法。

a Check the connection string of the project. b Go to services and restart SQLServer Instance. c Open 'SQLServer Configuration Manager' In the left panel select 'SQLServer Network Configuration' and expanding it Select 'Protocols for MSSQLServer' In the right panel dbl click on 'TCP/IP' In the 'Protocol' tab set the 'Enabled' to 'Yes' In the 'IP Addresses' tab scroll to down In the 'IPAll' set 'TCP Port' to 1433 d Open 'Firewall with advanced security' In the right tab select 'Inbound Rules'

在中间选项卡找到“本地端口”是1433的记录,如果你找不到它,尝试用以下级别创建它

In the Start menu, click Run, type 'WF.msc', and then click OK In the left panel click the 'Windows Firewall with Advanced Security' In the right panel right-click 'Inbound Rules', and then click 'New Rule' In the Rule Type dialog box, select 'Port', and then click Next In the Protocol and Ports dialog box, select 'TCP', and select 'Specific Local Ports', and then type the port number 1433, Click Next In the Action dialog box, select Allow the connection, and then click Next In the 'Profile' dialog box, checking the Domain, Private and Public, Then click Next In the 'Name' dialog box, type 'SQL 1433 Port' and for a description write description for own. Then click Finish

Then in the middle tab double click the found item(Instance) or created item of 'SQL 1433 Port' name by you. Select 'Scope' tab in opened dialog box(SQL Server Properties) In the Local PC Go to google.com in your Browser and search 'My IP'. then copy of your 'IP' Go to the remote server and in the 'SQL Server Properties' dialog box of 'Scope' tab, in the 'Remote IP Address' select the 'These IP Addresses' option and click 'Add' button In the opened dialog box(IP Address) select 'This IP Address or Subnet' option and paste your 'IP', click OK button.

如果您突然在生产环境中遇到这个错误,并且没有任何更改,请尝试以下顺序中的4个项目,以查看它是否得到修复。

重新启动SQL server服务。 重新启动调用sql server的服务(比如IIS)。(如果从开始对SQL server的服务调用到最终得到响应错误的时间非常短(大约一到两秒),那么问题可能就在这里。 重新启动服务器SQL server is on。 重新启动调用服务所在的服务器。