繁体   English   中英

ASP.NET C#SQL Server无法在调试中连接

[英]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进行连接的防火墙规则,每次更改后重新启动服务器,然后重新启动计算机。 基本上,所有内容都来自此线程 SQL Server与我正在测试的计算机位于同一台计算机上,所以我认为这不是远程连接问题。 关于Win32Exception有内部异常,但是我正在运行的所有内容都是64位。 也许这与它有关?

运行Windows 10,Visual Studio 2017社区版,SQL Server 2016 Express,相关代码如下:

字符串IacNum = txtIacNum.Text; 字符串CheckResult;

    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);

尝试从Server Explorer \\ Data Connections查看SQL Server。 如果可以创建连接,则将看到连接字符串属性。 然后,您可以进行比较,看看是否有相同的值-您可能有错字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM