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

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

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

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


当前回答

我使用的是SQL Server 2016和windows 10。

首先是允许远程连接到SQL Server。 我所做的就是键入sqlservermanager13。在开始菜单打开SQL Server配置管理器。确保TCP/IP状态为启用状态。

双击TCP/IP协议名称,检查TCP端口号。通常默认值是1433。

以下步骤通过使用Microsoft管理控制台(MMC)管理单元配置Windows防火墙的高级安全。高级安全Windows防火墙只配置当前配置文件。

To open a port in the Windows firewall for TCP access On the Start menu, click Run, type WF.msc, and then click OK. In the Windows Firewall with Advanced Security, in the left pane, right-click Inbound Rules, and then click New Rule in the action pane. In the Rule Type dialog box, select Port, and then click Next. In the Protocol and Ports dialog box, select TCP. Select Specific local ports, and then type the port number of the instance of the Database Engine, such as 1433 for the default instance. Click Next. In the Action dialog box, select Allow the connection, and then click Next. In the Profile dialog box, select any profiles that describe the computer connection environment when you want to connect to the Database Engine, and then click Next. In the Name dialog box, type a name and description for this rule, and then click Finish.

另一个需要配置的东西。

To open access to SQL Server when using dynamic ports On the Start menu, click Run, type WF.msc, and then click OK. In the Windows Firewall with Advanced Security, in the left pane, right-click Inbound Rules, and then click New Rule in the action pane. In the Rule Type dialog box, select Program, and then click Next. In the Program dialog box, select This program path. Click Browse, and navigate to the instance of SQL Server that you want to access through the firewall, and then click Open. By default, SQL Server is at C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Binn\Sqlservr.exe. Click Next. In the Action dialog box, select Allow the connection, and then click Next. In the Profile dialog box, select any profiles that describe the computer connection environment when you want to connect to the Database Engine, and then click Next. In the Name dialog box, type a name and description for this rule, and then click Finish.

看看微软的文档 配置Windows防火墙访问数据库引擎

其他回答

按window + R(运行窗口打开),在运行窗口类型“服务”。msc”和新服务打开找到实例名SQL SERVER在我的情况下,它是SQL SERVER(SQLEXPRESS),然后启动这个服务,再试一次,它为我工作,希望它也为你工作。

我也遇到过同样的问题 就我而言,我用这种方法解决了问题

步骤1:从开始菜单进入SQL server配置管理器

步骤2:启用TCP/IP

步骤3:双击TCP/IP,进入IP地址最后一个条目IP ALL,输入TCP端口1433然后应用

第四步:然后按win+r,写服务。msc打开服务,然后向下滚动,然后右键单击SQL Server (MSSQLSERVER)选择重新启动

这解决了我的问题。 即使做以上所有步骤都不能解决问题,然后简单地重新启动PC,希望它会工作。

在做了上面提到的所有事情之后: 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而不是使用这个服务器名。

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

谢谢。

我尝试了关于这个问题的所有其他答案,其中一些(如果不是全部的话)可能在让这个为我工作方面发挥了作用,但我仍然无法远程连接到DB。我在Azure虚拟机上使用SQL Server。

我最终想起了VM的端点是由Azure帐户代理控制的,所以我继续使用Azure Portal并添加1433作为可用端点,这样我就可以连接到我的SQL实例了。

希望这能帮助那些尝试了所有其他答案但仍然没有运气的人!

为什么这个错误是如此烦人和嘈杂,只是因为它可以发生在不同的情况。

我已经做了上面所有的方法,但还是很糟糕。所以,在向下浏览之前,确保你和我做了同样的事情。

也许我不能立即解决你的问题,但我可以给你指明一个方向或思路(最后滑下来的那个人)。我已经开始思考我的运行程序的错误发生后,我确保实例名是明确正确的,并设置我的数据库,以允许远程控制以下的方法。之后,我怀疑我的SQL连接代码片段发生了错误。

我的问题解决方案:

检查我的sqlconnection函数

单击以查看其配置

新建连接

选择你的服务器名

对我来说,思考在联系的过程中到底发生了什么是有效的。希望我的想法能引导你改正错误。