.NET Core无法连接到远程SQL Server数据库 [英] .NET Core can't connect to remote SQL Server database
问题描述
我有一个.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"
}
I可以通过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
推荐答案
尝试以下:
- 确保通过SQL Server配置管理器启用了TCP / IP协议。另外,请确保未配置自定义端口,获取详细信息
- 执行
telnet HC-SERVER-04 1433
从远程主机的命令提示符处(您尝试连接SQL Server的位置)
- Make sure TCP/IP protocol enabled via SQL Server configuration manager. Also, ensure the custom port not configured, for detailed info
- Do
telnet HC-SERVER-04 1433
from command prompt of the remote host (from where you tried to connect SQL server)
如果步骤2 失败:在端口号为 1433
和<$ c $的SQL Server防火墙中创建规则c> 1434 接受传入的连接,然后重试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
- 如果在进入防火墙后仍不起作用:重新启动
SQL浏览器
服务 - <$ c没运气$ c> SQL Browser 服务,如下更改连接字符串:
- If doesn't work after firewall entry: Restart
SQL Browser
service - 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屋!