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

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

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

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


当前回答

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

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

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

其他回答

当我在Visual Studio中遇到这个错误时,

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

...它是在执行下面的c#代码期间,它试图获取我的SQL Server数据以在网格中显示。中断恰好发生在表示connect.Open()的行上:

        using (var connect = Connections.mySqlConnection)
        {
            const string query = "SELECT Name, Birthdate, Narrative FROM Friends";
            using (var command = new SqlCommand(query, connect))
            {
                connect.Open();
                using (var dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        // blah
                    }
                }
            }
        }

It was inexplicable because the SQL query was very simple, I had the right connection string, and the database server was available. I decided to run the actual SQL query manually myself in SQL Management Studio and it ran just fine and yielded several records. But one thing stood out in the query results: there was some improperly encoded HTML text inside a varchar(max) type field within the Friends table (specifically, some encoded comment symbols of the sort <!-- lodged within the "Narrative" column's data). The suspect data row looked like this:

Name    Birthdate    Narrative
====    =========    ============== 
Fred    21-Oct-79    &lt;!--HTML Comment -->Once upon a time...

注意编码的HTML符号“&lt;”,它代表一个“<”字符。不知何故,它进入了数据库,而我的c#代码无法接收它!它每次都失败在connect.Open()行!在我手动编辑数据库表Friends中的一行数据并输入解码后的“<”字符之后,一切都正常了!这一行应该是这样的:

Name    Birthdate    Narrative
====    =========    ============== 
Fred    21-Oct-79    <!--HTML Comment -->Once upon a time...

我使用下面这个简单的UPDATE语句编辑了一个不好的行。但如果你有几行违规的HTML编码,你可能需要一个更详细的UPDATE语句,使用REPLACE函数:

UPDATE Friends SET Narrative = '<!--HTML Comment -->Once upon a time...' WHERE Narrative LIKE '&lt%'

因此,这个故事的寓意是(至少在我的情况下),在将HTML内容存储到数据库之前清理它,这样您就不会在第一时间得到这个神秘的SQL Server错误!(如果你需要更多信息,正确地消毒/解码你的HTML内容是另一个值得单独在StackOverflow搜索的讨论主题!)

我使用的是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防火墙访问数据库引擎

验证用于为SQL Server实例创建会话的帐户。

把我的高度好评的评论作为一个截图的回答。

我在这上面花了很多时间,最后对我有用的是:

1)打开Sql Server Configuration Manager—> Sql Server Network Configuration—> Protocols for <(INSTANCE)>—> TCP/IP(双击它)。

2)选择“——> IP Addresses(Tab)”。

3)转到最后一个条目IP All,并提到TCP端口1433。

4)按“Win+R”,输入services.msc。

5)现在重启SQL Server <(INSTANCE)>。

在这之后,问题解决了!

我遇到了同样的问题,问题是我在解决方案(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和默认项目后,我得到了它的工作。