Azure SQL DB connection issues from C#

I'm having trouble connecting with my .NET 5 web app to an Azure SQL DB. The weird thing is that I can connect to the DB using Azure Data Studio.

I've configured the Azure SQL Server firewall to accept connections from my IP address, and all of the Inbound/Outbound IP addresses of my Azure App Service. Before adding my local IP address to the firewall, I could not connect with Azure Data Studio, so that works as intended, but then I use the connection string provided by Azure itself (in ADO.NET format, since I'm using Entity Framework) inside my web app, and the app can't connect to the DB (it times out with 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: 40 - Could not open a connection to SQL Server)).

I can't really understand why my localhost instance can't connect, as the IP address trying to connect to the db is the same as when I use Azure Data Studio! And also the App Service instance in Azure can't connect either (in the SQL Server's firewall I've also enabled connectivity from Azure App Services!!). The connection string is of the form

Server=tcp:[my server];Initial Catalog=[my db];Persist Security Info=False;User ID=[my username];Password=[my password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

Any hints?

1 answer

  • answered 2021-10-25 05:30 Shiraz Bhaiji

    Since you can access the server from the same machine, it is not a firewall issue.

    There are two possibilities you could check:

    1. The connection string that your program is using is not what you expect. Try logging the connection string.
    2. The server is configured to use named pipes and not tcp. Azure Data Studio is configured to use named pipes and therefore works. Try checking the server configuration.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum