.NET Core 无法连接到远程 SQL Server 数据库 [英] .NET Core can't connect to remote SQL Server database

查看:109
本文介绍了.NET Core 无法连接到远程 SQL Server 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Core 应用程序作为 API.我可以将它连接到本地 SQL Server 数据库,但不能连接到我网络上的其他数据库.

I have a .NET Core application as API. I can connect it to a local SQL Server database, but not to other databases on my network.

当我尝试使用 dbContext 访问远程数据库时,出现此错误:

When I try to access a remote database using a dbContext, I get this error:

System.Data.SqlClient.SqlException:
与 SQL Server 建立连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接.(提供程序:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

System.Data.SqlClient.SqlException:
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)

appsettings.json 用于本地:

  "ConnectionStrings": {
    "DefaultConnection": "Data Source=(localdb)\mssqllocaldb;Initial Catalog=ib;Integrated Security=True;"
  }

appsettings.json 用于远程:

  "ConnectionStrings": {
    "DefaultConnection": "Server=[servername][instanz];Database=IBCore_Lange;User Id=sa;password=XXX"
  }

我可以通过 SQL Server Management Studio 或我系统的以前 (ASP.NET) 版本访问数据库.在 Core 中,我只能访问本地数据库.

I can access the database via SQL Server Management Studio, or a previous (ASP.NET) version of my system. In Core, I can only access to a local database.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

    services.AddDbContext<ibContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

DbContext 生成:

Scaffold-DbContext "Connection String"
         Microsoft.EntityFrameworkCore.SqlServer -Verbose -o {output folder} -t {table to update} -force

推荐答案

尝试以下操作:

  1. 确保通过 SQL Server 配置管理器启用 TCP/IP 协议.另外,请确保未配置自定义端口,了解详细信息
  2. 从远程主机的命令提示符执行 telnet HC-SERVER-04 1433(从您尝试连接 SQL 服务器的位置)
  1. Make sure TCP/IP protocol enabled via SQL Server configuration manager. Also, ensure the custom port not configured, for detailed info
  2. Do telnet HC-SERVER-04 1433 from command prompt of the remote host (from where you tried to connect SQL server)

如果步骤 2 失败:在 SQL Server 的防火墙中使用端口号 14331434 创建规则以接受传入连接,然后尝试再次 telnet,如果 telnet 工作,应用程序应该能够连接

If Step 2 fails: Create rule in Firewall of the SQL Server with port# 1433 and 1434 to accept incoming connections, then try again telnet, if telnet works, application should be able to connect

  1. 如果进入防火墙后还是不行:重启SQL Browser服务
  2. SQL Browser 服务不走运,更改连接字符串如下:
  1. If doesn't work after firewall entry: Restart SQL Browser service
  2. No Luck with SQL Browser service, change connection string as follows:

Server=HC-SERVER-04,1433;Database=IBCore_Lange;User Id=sa;password=XXX

这篇关于.NET Core 无法连接到远程 SQL Server 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆