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

Notifications

Announcements

No record found.

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
    237,795 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans