Finding the Server Name and Database Name

darrencdarrenc Member Posts: 16
Does anyone know how to retrieve the server name and database name in code? I need to display this on a report.

Thanks
Darren

Comments

  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    You can use this How To to retrieve the servername: How To retrieve servername and servertype?
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • darrencdarrenc Member Posts: 16
    Thanks Luc. The CONTEXTURL has the database in it also. I should be able to parse it out.
  • PollekePolleke Member Posts: 18
    If you are using SQL-server then you can use the virtual table "Server" to retrieve the servername, and the virtual table "Session" to retrieve the databasename (but this is a performance killer).
  • pdjpdj Member Posts: 643
    Regards
    Peter
  • RobertMoRobertMo Member Posts: 484
    On most DBs I have following function:
    PROCEDURE ADGetServerDBInfo@1000000001(ltWhatToGet@1000000003 : Text[30]) ltReturnName : Text[250];
    VAR
      ltTemp@1000000004 : Text[250];
      liPos@1000000005 : Integer;
      lrSession@1000000000 : Record 2000000009;
    BEGIN
      // ADGetServerDBInfo
      //-AD.RM
      { Usage:
      Call ADGetServerDBInfo('servername=') to Get Server Name
      Call ADGetServerDBInfo('servertype=') to Get Server Type
      Call ADGetServerDBInfo('database=') to Get Database Name
      Call ADGetServerDBInfo('company=') to Get Company Name
      }
      ltReturnName := '<unknown>';
      ltTemp := CONTEXTURL;
      liPos := STRPOS(ltTemp, ltWhatToGet);
      IF liPos > 0 THEN BEGIN
        ltTemp := COPYSTR(ltTemp, liPos + STRLEN(ltWhatToGet));
        liPos := STRPOS(ltTemp, '&');
        IF liPos = 0 THEN liPos := 999;
        ltReturnName := COPYSTR(ltTemp, 1, liPos - 1);
      END ELSE BEGIN
        CASE ltWhatToGet OF
          'servername=' : ltReturnName := '<local>';
          'database=' : BEGIN
             lrSession.SETRANGE(lrSession."My Session", TRUE);
             IF lrSession.FIND('-') THEN ltReturnName := lrSession."Database Name";
          END;
        END;
      END;
      EXIT(ltReturnName);
      //+AD.RM
    END;
    
    I have this function on Main Menu with 3 text variables:
    tServerName Text250
    tServerType Text250
    tDatabaseName Text250
    
    ...that I use them as source Expression for 3 barely visible fields at bottom. So I always know on which DB I'm working...
    It looks like...
    http://freeweb.siol.net/saromo/ADGetServerDBInfo.jpg
               ®obi           
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sign In or Register to comment.