c# - 将应用程序 Web 部署到单独的 Web 服务器后出现 SQL Server 错误

标签 c# sql-server asp.net-mvc entity-framework azure

将应用程序移至 Prem 上其自己的 Windows 2012 R2 IIS Web 服务器后,我收到以下错误。 SQL 2014 DB 服务器位于 Azure 中。防火墙已禁用。

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)

进一步:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist.

我的连接字符串未指定任何本地 SQL Express DB:

 <connectionStrings>
    <remove name ="NewUserToolContext"/>
    <remove name ="DefaultConnection"/>
    <add name="NewUserToolContext" connectionString="Data Source=SQLDBSERVERNAME;Initial Catalog=NewUserTool;Integrated Security=False;User Id = USERID;password = PASSWORD" providerName="System.Data.SqlClient" />
    <add name="DefaultConnection" connectionString="Data Source=SQLDBSERVERNAME;Initial Catalog=NewUserTool;Integrated Security=False;User Id = USERID;password = PASSWORD" providerName="System.Data.SqlClient" />
  </connectionStrings>

我的部分 _Layout View 有一些代码,用于在显示特定菜单项之前检查角色成员身份。经过大量搜索后,我发现了 2012 年的旧帖子和 2013 年的一篇关于某种成员资格初始值设定项的帖子。 The post非常模糊和令人困惑,而且似乎不相关。它也没有解释任何要做什么,尤其是 2017 年的 MVC 6 Entity Framework 。我尝试过,但找不到他们在谈论的任何内容。我去年开始使用 MVC 6 学习 ASP.NET。

最佳答案

首先,您是否在虚拟机的防火墙中启用了 1433 端口?如果没有,您可以使用以下步骤打开此端口。

  1. 在“开始”菜单上,单击“运行”,键入 WF.msc,然后单击“确定”。
  2. 在高级安全 Windows 防火墙的左 Pane 中,右键单击“入站规则”,然后单击操作 Pane (右上角)中的“新建规则”。
  3. 在“规则类型”对话框中,选择“端口”,然后单击“下一步”。
  4. 在“协议(protocol)和端口”对话框中,选择 TCP。选择“特定本地端口”,然后键入数据库引擎实例的端口号,默认 SQL Server 使用 1433。请输入 1433,然后单击“下一步”直至完成步骤。

其次,您还需要在与您的虚拟机相关的网络安全组中启用 1433 端口。以下步骤供您引用。

  1. 在网络界面面板中找到与您的虚拟机相关的网络安全组名称。

enter image description here

  • 打开此 NSG,单击入站安全规则面板中的[添加]按钮
  • enter image description here

  • 添加如下规则。
  • enter image description here

  • 之后,您就可以访问虚拟机中托管的数据库了。
  • 编辑2017/6/13

    首先,我建议您在服务器上使用连接字符串之前将其写入日志文件。您将发现是否使用正确的连接字符串来连接您的服务器。也许连接字符串被其他配置覆盖。

    此外,您的 Web 服务器是托管在 Azure VM 还是本地计算机上?

    另请尝试在应用程序池高级设置中启用 32 位应用程序。

    enter image description here

    您可以尝试其他一些事情。

    1. 请检查您是否配置了Dynamic IP Security或 web.config 中的 ipSecurity。它将根据您的配置阻止任何 IP 地址。

    2. 在部署版本时,您是否尝试过删除测试服务器上的所有原始文件?

    enter image description here

    关于c# - 将应用程序 Web 部署到单独的 Web 服务器后出现 SQL Server 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395991/

    相关文章:

    c# - 如何在 C# 中存储两个二进制字符串并使用 OR 运算符

    c# - 使用 System.Runtime.Serialization.Json 反序列化 JSON

    sql-server - 如果子查询返回太多数据,我应该担心吗?

    c# - 使用children更新数据库条目

    c# - await UserManager.CreateAsync 查找不存在的列

    c# - 将 C# 项目迁移到 .NET 4.0 隐藏的问题和错误

    javascript - 如何在asp.net中更新gridview文本框值

    sql - 在 SQL Server 2012 中减去前一行值

    SQL:使用 EXCEPT 时如何获取 COUNT

    asp.net-mvc - 在 ASP.NET MVC 中,返回 RedirectToAction 时保留 URL