ASP.NET C# SQL 服务器未在调试中连接

ASP.NET C# SQL Server not connecting in debug

每当我调试 ASP.NET Web 表单时,我都会收到以下错误:

System.Data.SqlClient.SqlException occurred HResult=0x80131904
Message=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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Source=.Net SqlClient Data Provider
StackTrace:
<Cannot evaluate the exception stack trace>

Inner Exception 1:
Win32Exception: The network name cannot be found

我研究了很多与错误 40 相关的内容,并尝试了所有推荐的方法。启用 TCP/IP,将端口设置为 1433,创建允许通过 TCP/IP 端口 1433 连接的防火墙规则,每次更改后重新启动服务器,然后重新启动计算机。基本上,从 this thread 开始的一切。 SQL 服务器在我测试的同一台机器上,所以这不是远程连接问题,我不认为。有关于 Win32Exception 的内部异常,但我 运行 的所有内容都是 64 位的。也许这与它有关?

运行Windows10,Visual Studio2017 Community Edition,SQLServer 2016 Express,相关代码如下:

字符串 IacNum = txtIacNum.Text; 字符串检查结果;

    String queryString = "SELECT ExpirationDate FROM [dbo].[IACList] WHERE ApprovalNumber = @IacNum";
    String connectionString = "Data Source=PCNAME/SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";

    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand command = new SqlCommand(queryString, connection);
    command.Parameters.AddWithValue("@IacNum", IacNum);
    command.Connection.Open(); //Exception pops up here
    var reader = command.ExecuteScalar();
    if(reader != null)
    {
        CheckResult = "Valid. Expires" + reader;
    }
    else
    {
        CheckResult = "Invalid";
    }
    command.Connection.Close();
        MessageBox.Show(this, CheckResult);

尝试从服务器 Explorer\Data 连接查看 SQL 服务器。如果可以创建连接,您将看到一个连接字符串 属性。您可以比较一下,看看您的值是否相同——您可能有错字。