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

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

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

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


当前回答

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

其他回答

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.

原来在升级后,服务的“Log On”属性(服务属性上的第二个选项卡)被设置为NT AUTHORITY\NETWORK SERVICES。当我将其更改为“本地系统帐户”时,我能够启动服务并连接到MS SQL 2019实例。

这个msdn论坛的帖子帮助我找到了这个。

尝试在连接字符串的末尾添加一个和一个端口号(如1433)。

azure vm用户

我已经尝试了上面所有的(所以这些仍然可能是有效的),但仍然没有wotk。

重要提示:我的只是一个游乐场服务器,所以安全性真的不是问题。所以你要做好调查,这只是一种变通方法,不是安全的方法。

确保可以ping通服务器。在我的情况下,我可以RDP,但不能ping我的服务器在Azure上。我的请求总是超时。因为似乎Azure默认阻止ICMP协议。

您可以在防火墙中添加规则。在我的例子中,无论我在那台机器上通过防火墙添加了什么,都没有任何改变。感谢@John的上述回答,这也提醒了我,我必须从Azure门户上的网络添加这个。

只要我为端口0添加了规则,我就可以远程连接了。

这个链接这个链接也很有用。

如果SQL Server正在运行,您仍然得到一个错误; 当打开SQL Server时,显示的不是默认的服务器名 选项下选择以\SQLEXPRESS结尾的服务器名 服务器名称->浏览更多信息->数据库引擎选项。

当我在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搜索的讨论主题!)