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 :
Business Central forum

How to connect and execute SQL scripts from AL Language, The alternative method without using .NET variable.

(0) ShareShare
ReportReport
Posted on by

We are Microsoft ISV, our main engine is SQL Stored Procedure, We were using .NET variable to connect to SQL from C/AL Code, not MS removed .NET variable for security reason, Could you advise on the alternative solution, Like connect it through REST Web API with JSON etc.. I could not get the proper document or steps for this.

Categories:
  • Suggested answer
    I Gusti Made Ari Profile Picture
    3,608 on at

    Consuming REST Web API is now using HttpClient in AL.

    You can find the detail on below link :

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-restapi-overview

    and you can see some example in here :

    https://www.kauffmann.nl/2017/07/18/al-web-service-examples/

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    Just to add a note: if you're on-premise, you can still use DotNet and access to SQL Server directly. On SaaS the world is different, the database layer is not visible and you need to use web services or APIs as described before.

  • Community Member Profile Picture
    on at

    Hello Stefano Demiliani, Thanks for responding with my query, Yes now I understood the architecture of on premises and cloud, but now I am stuck with on how to create the REST API web service for online D365 BS, I have coded and tested the REST API using ADO.NET in on premises(SQL Server) it works perfectly, as wont be able to know the connection string(Azure SQL Server) for online version I am not sure how to create the connection without knowing the connection string. Please kindly advise.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    REST API and ADO.NET are two different things. With ADO.NET you perform a direct connection with the database and so you need a connection string.

    In SaaS you cannot use ADO.NET but you need to use web services (SOAP) or REST API (link above). With REST API you send an HTTP Request (GET, POST etc.) to an URL and you receive an HttpResponse object as result.

  • Community Member Profile Picture
    on at

    Stefano Demiliani, Thank you! but I could not find the perfect example for REST API which connects to SQL and executes the query using HTTP Request/Response. Could please share if you have one.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    This is a piece of C# code that comes from a my program:

    HttpClient client = new HttpClient();

    client.BaseAddress = new Uri(baseURL);

    client.DefaultRequestHeaders.Accept.Add(

                   new MediaTypeWithQualityHeaderValue("application/json"));

    string userAndPasswordToken =

                   Convert.ToBase64String(Encoding.UTF8.GetBytes(user + ":" + key));

               client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization",

                   $"Basic {userAndPasswordToken}");

    HttpContent httpContent = new StringContent(CUSTOMERJSONOBJECT, Encoding.UTF8, "application/json");

               HttpResponseMessage response = await client.PostAsync(baseURL + "/companies(" + companyID + ")/customers", httpContent);

    Here CUSTOMERJSONOBJECT is a JSON of a customer to create on D365BC.

  • Community Member Profile Picture
    on at

    Hi Stefano Demiliani, Thanks, But my scenario is this, We have a many stored procedures in SQL and basically we want to pass the parameter, execute the stored procedure and return the results back to business central, Do you have any example with SQL connection.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    If you’re on-premise and you need to access SQL Server for starting stores procedures, you have to use DotNet and referencing System.Data.SqlClient namespace. An example here:

    [View:https://rockwithnav.wordpress.com/2016/02/15/call-stored-procedure-from-dynamics-nav-dotnet-variable/]

    Remember that DotNet variables are always available for the on-premise version, only on the SaaS platform they’re not available.

  • Community Member Profile Picture
    on at

    Hi Stefano Demiliani, This is what we have been doing it on premises, Now I am exploring the solution for SaaS, I also heard that the RTC client will not be available soon, Dot Not variable wont work without RTC probably. Could you send me any example for SaaS if you have please.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    Sorry but all what you need to know is explained in this thread. As said before, on SaaS you cannot call stored procedures.

    I don't have clear your scenario:

    1) do you have an external database where you have stored procedures and you want to call them from  D365BC for having results?

    2) do you want to create stored procedures inside the D365BC database?

    3) do you want query a D365BC database from an external application?

    Answers to these 3 scenarios:

    1) Not possible. A SaaS environment cannot directly see your local db. My suggestion: create a web service or an API and call it from D365BC using HttpClient object.

    2) Not possible (no access to the db)

    3) You have to use web services or APIs exposed by D365BC.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans