How to get name of database server in AL w/o dll usage?

MarHanMarHan Member Posts: 34
Hi community,

is there a way to determine the name of the sql database server in AL without using dlls? I need the database name which I should get from the active session, but I have no clue how to get the database server name - the active session only carries the name of the BC server.

Thanks in advance and best regards

Markus

Answers

  • vaprogvaprog Member Posts: 1,139
    I don't know about AL on Azure, but from C/AL OnPremise I use Microsoft.Dynamics.Nav.Types.ServerUserSettings. Microsoft.Dynamics.Nav.Types.dll is part of any Dynamics NAV / BC installation. For development it needs to be placed somewhere in the path, or in the Add-ins folder. It then can be used with a standard installation without adding or modifying anything. No need to move the dll out from it's installation folder alongside the .exe.

    I use ServerName() for the SQL server name. I am not sure if this includes the instance or not, but I believe it does.

    I use DatabaseName.ToString for the database name on the above server.

    I have not found an other way that works. There are many suggestions about retrieving this data from the server .config file. I have not found any way of detecting your service instance and the path to that instance's configuration, so all those instructions only work with the default instance, and probably don't work on Azure at all.

    Try if this helps, and please report back, for the benefit of others.
  • MarHanMarHan Member Posts: 34
    Hi vaprog,

    this is a bit like we currently are doing it but we must get rid of this due to the universal code initiative where we're no longer allowed to use dlls even on-premises. A lot of stuff can already be done natively from AL but we didn't find a way for that yet. That's why I'm asking :-)
  • MarHanMarHan Member Posts: 34
    Or does anyone has a clever way to determine on-premises that the current system is a copy of the production database?

    We have a lot of customers that frequently create .bak backups from their production system via SQL to restore them as new test environments. The so created new test system must not behave like the production system (importing files, sending out emails, ...). Therefore we store and compare a token of a combination of database server name and database instance name with the current ones and recognize that database server and instance name has changed. If so, a lot of stuff gets disabled.

    I know that in Saas, there are events to regognize the creation of sandboxes for example. But we didn't find other ways to do it on-premises. Ideas welcome :-)
  • AdamRobAdamRob Member Posts: 1
    Try this.

    ActiveSession.Reset();
    ActiveSession.FindFirst();
    Message('The database name is %1',ActiveSession."Database Name");

    Note: Avoid doing a get like below, as I found this did not work 100% of the time. ActiveSession.Get(Database.ServiceInstanceId(), Database.SessionId())
  • MarHanMarHan Member Posts: 34
    edited 2024-05-03
    As already mentioned, this gives me only the name of the sql database but not the name of the sql database server.

    When a backup of the production sql database gets restored on a separate database server with the same database name it's impossible to recognize that it is not the production database. So we decided to take the combination of database server name plus database name into account for determining that.

Sign In or Register to comment.