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

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

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

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


当前回答

我的问题开始于我试图将服务器从IIS Express更改为本地IIS(同时使用LocalDB)。

我正在使用LocalDB(用于开发目的),当我从本地IIS恢复到IIS Express时,Visual Studio已经将我的数据源从数据源=(LocalDB)\MSSQLLocalDB切换到数据源=.\SQLEXPRESS

错误的连接字符串

< add name = " DefaultConnection connectionString =“源代码=数据。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ SurveyTestsDB。中密度纤维板;最初用户目录= SurveyTestsDB集成安全= True;例如= True” providerName =“系统数据。SqlClient " - >

正确的连接串

< add name = " DefaultConnection connectionString =“源代码=数据(LocalDb) \ MSSQLLocalDB; AttachDbFilename = | DataDirectory | \ SurveyTestsDB。中密度纤维板最初目录》中= SurveyTestsDB;集成安全= True”providerName =“系统数据。SqlClient " - >

希望这能帮助到一些人。

其他回答

如果您正在移除迁移,您应该检查ConnectionString。

您可以测试以下方法。

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.

我必须运行SQL Server浏览器服务到SQL Server配置管理器。 如果没有这个,安装就不能发现新创建的服务。

当我在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端口1433到Windows防火墙例外。请执行以下步骤。

进入开始菜单,打开运行类型WF.msc。 在Windows防火墙高级安全。 添加入站规则。 添加1433端口。 说出你的规则。 重新启动SQL服务。

有关连接远程服务器的问题的完整指南。处理连接SQL Server数据库的故障。