web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

System.Data.SqlClient.SqlConnection failed on Azure SQL environment

(0) ShareShare
ReportReport
Posted on by 25

Hi All,

Recently I solved quite tricky issue happened on D365 Test environment running in Azure.

Original task was developed and tested on local D365 environment and works well, but suddenly failed on client's environment with a message:

Exception has been thrown by the target of an invocation.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.)No such host is known
Exception has been thrown by the target of an invocation.
ExecuteReader requires an open and available Connection. The connection's current state is closed

The issue was that SQLSystem.loginServer() executed on Dynamics 365 for Financials and Operations connected to Azure SQL Database, returned only server name, without a full domain part "database.windows.net"

So here is working example of getting SQLClient.SQLConnection that works both with MS SQL and Azure SQL.

private System.Data.SqlClient.SqlConnection GetSQLClientConnection()
    {
        System.Data.SqlClient.SqlConnection con;
        SQLSystem sql;
        str fullServerName;
        str loginConnectString, noDriverConnectString;
        int beginPos, endPos;

        try
        {
            new InteropPermission(InteropKind::ClrInterop).assert();
            sql = new SQLSystem();
            loginConnectString = sql.loginConnectString();
            beginPos = strScan(loginConnectString, "Driver=", 0, strLen(loginConnectString));
            endPos = strScan(loginConnectString, ";", beginPos, strLen(loginConnectString));
            noDriverConnectString = subStr(loginConnectString, 1, beginPos-1);
            noDriverConnectString = subStr(loginConnectString, endPos + 1, strLen(loginConnectString));
            noDriverConnectString = strRem(noDriverConnectString, "{}");
            con = new System.Data.SqlClient.SqlConnection(noDriverConnectString);
            con.Open();
        }
        catch (Exception::CLRError)
        {
            //BP Deviation Documented
            ex = ClrInterop::getLastException();
            while(ex)
            {
                exceptionStr += ex.get_Message();
                ex = ex.get_InnerException();
            }
            info(exceptionStr);
        }

        return con;
    }

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Andy Shevchenko Profile Picture
    25 on at

    solved by author :)

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Please share the solution and mark the solution as the verified answer. The current verified answer isn't useful for anybody who'll have the same question and will find this answered thread. Thank you!

  • Andy Shevchenko Profile Picture
    25 on at

    Hi Martin,

    Code shown in the question is working solution.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans