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
    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
    47,773 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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics NAV (Archived)

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans