问题
I have tried running this in a asp.net core 3.1 app inside a docker container based on mcr.microsoft.com/dotnet/core/sdk:3.1-bionic (Ubuntu 18.04). also tried with mcr.microsoft.com/dotnet/core/sdk:3.1-buster.
I'm trying to connect to a database with the following code:
using var connection = new SqlConnection(connectionString);
connection.Open();
If i use an ip adress it works fine. If i run this outside of a docker container it works fine. If i run in a container and use System.Data.SqlClient 4.4.3 or higher and use a hostname this will fail with the following error:
"Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or in stance-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: TCP Provider, error: 40 - Could not open a connection to SQL Server)"
My first though was that it was an issue with DNS resolution so i tried Dns.GetHostAddresses("myhostname");
but it is able to resolve the correct IP from it,
so that does not seem to be the issue. If i downgrade to System.Data.SqlClient 4.4.2 my application has no issue connecting to the sql server with hostname. So what change was made in 4.4.3 that could make me have these issues? I can also mention that i tried with the following libraries:
**Microsoft.Data.SqlClient v2.0.0**,
**System.Data.SqlClient 4.8.1**,
**Microsoft.EntityFameworkCore.SqlServer**.
Same issue. I tried using sqlCmd inside the docker container and it has no problem connecting.
I tried this with two different databases hosted in different places and get it working with one but not the other. I suspect that it is something else than the actual version of software on the hosting machine that is the issue but posting info anyway for completeness. Works with hostname only if i use SqlCLient version 4.4.2, ip can be used with 4.4.3 and above.
l Microsoft SQL Server 2016 (SP2-GDR) (KB4532097) - 13.0.5102.14 (X64)
Dec 31 2019 22:39:35
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows Server 2016 Datacenter 10.0 <X64>
(Build 14393: ) (Hypervisor)
Works with hostname regardless of SqlCLient version
Microsoft SQL Server 2016 (SP2-GDR) (KB4532097) - 13.0.5102.14 (X64)
Dec 31 2019 22:39:35
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Datacenter 6.3 <X64>
(Build 9600: ) (Hypervisor)
Even if some network configuration on the sever side could be a contributing factor to me having this issue, The one factor that remains is that it works with System.Data.SqlClient 4.4.2 but fails with anything above that version. I would really like to understand what is happening. Here is a minimal project that can reproduce the issue. Very basic, so i don't think it is anything in my code that is directly an issue. sql-test.zip
来源:https://stackoverflow.com/questions/62899093/unable-to-connect-to-sql-database-using-system-data-sqlclient-4-4-3-and-hostname