Got this error when establishing connection to Azure SQL Database:
Microsoft.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: TCP Provider, error: 0 - No such host is known.)
Inner Exception:
Win32Exception: No such host is known.
Here is the code:
using Microsoft.Data.SqlClient;
var builder = new SqlConnectionStringBuilder
{
IPAddressPreference = SqlConnectionIPAddressPreference.IPv4First,
//IPAddressPreference = SqlConnectionIPAddressPreference.IPv6First,
DataSource = "<name_of_the_server>.database.windows.net",
InitialCatalog = "<name_of_the_db>",
Authentication = SqlAuthenticationMethod.ActiveDirectoryIntegrated,
UserID = "<my_email>",
};
var connectionString = builder.ConnectionString;
using SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
What's more, I also got similar error when connection through Azure Data Studio:
Microsoft.Data.SqlClient.SqlException (0x80131904): 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: TCP Provider, error: 0 - No such host is known.
To make sure my IP is allowed by firewall, I manually added my IPv4 through Azure Portal and IPv6 through azure sql server ipv6-firewall-rule
command. But things did not help, still got such error with either SqlConnectionIPAddressPreference.IPv4First
or SqlConnectionIPAddressPreference.IPv6First
.
I am 100% for sure that the target SQL server exists. What reason might it be and how to fix this?
Update: during this period, through "Metrics" section in Azure portal, the "Blocked by firewall" is constantly zero and "Availability (Preview)" is constantly 100%.