Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Is it possible to access SQL from C/AL?

(0) ShareShare
ReportReport
Posted on by 179

I need to get data from another databases. I tryied using linked objects and it works but it is too slow and expensive as I need a table for each view.

Is it possible to connect to a SQL Server and run a command? Maybe with an automation or DotNet variable?

It would be perfect if I can tun this: Select * from MyTable and read the rows. So I can generate a temporaly table and run a page.

Thanks.

*This post is locked for comments

  • Jaime Figueras Profile Picture
    Jaime Figueras 179 on at
    RE: Is it possible to access SQL from C/AL?

    Thanks for your reply. It sound great. What if I cant Find System.Data.Variables...?

    7446.Nav.jpg

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 44,656 on at
    RE: Is it possible to access SQL from C/AL?

    Yes, you can connect to SQL and execute the query or stored procedure. Below is an example using DotNet "'System.Data Variables

    GetSQLConnectionString returns the connection string

    'Data Source=' + "SQL Server Name" + ';'
    
                             + 'Initial Catalog=' + "SQL Database Name" + ';'
    
                             + 'Uid=' + "SQL UserID" + '; '
    
                             + 'Pwd=' + "SQL Password" + ';'
    
    SQLConnection    := SQLConnection.SqlConnection(GetSQLConnectionString);
    
         SQLCommand := SQLCommand.SqlCommand();
    
         SQLCommand.Connection := SQLConnection;
    
         SQLConnection.Open;
    
         SQLCommand.CommandText :=  'sp_abcd';
    
         SQLCommand.CommandType := SQLCommand.CommandType.StoredProcedure;
    
         SQLCommand.CommandTimeout := 120;
    
        // If you have parameters you need to add parameters using the SQL Parameter      
    
         SQLParameter := SQLParameter.SqlParameter;
    
         SQLParameter.ParameterName := '@CustomerNo';
    
         SQLParameter.SqlDbType := SQLParameter.SqlDbType.VarChar;
    
         SQLParameter.Direction := SQLParameter.Direction.Input;
    
         SQLParameter.Size := 50;
    
         SQLParameter.Value:=CustNo;
    
         SQLCommand.Parameters.Add(SQLParameter);
    
         SQLCommand.ExecuteScalar;
    
         SQLConnection.Close;
    
         SQLConnection.Dispose;


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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans