How can I get the name of the database a DBExpress

2019-09-02 19:31发布

问题:

I'm testing a quite old Delphi 6 application and would like to display the database name the TSqlConnection is actually connected to, so I can see quickly if I'm connected to the test or production database.

In sqlconnections.ini, the app has a connection named 'Vienna' to a Firebird database defined like this: Database=192.168.1.15:ProductionDB (it's an alias) and I've replaced that for testing purposes with Database=192.168.1.15:TestDB.

But I've seen that just accessing the TSqlConnection's Params-List and there the value of 'Database' does not work. This value is always set the same as it is in design mode.

How can I find out which database (which Firebird alias in my case) the TSqlConnection is actually connected to?

回答1:

monitoring tables were introduced into FB 2.1.x :-)

So try

 select MON$DATABASE_NAME from MON$DATABASE

Or try

 select MON$ATTACHMENT_NAME from MON$ATTACHMENTS
    where MON$ATTACHMENT_ID = CURRENT_CONNECTION

See info at

  • c:\Program Files (x86)\Firebird\Firebird_2_1\doc\README.monitoring_tables.txt
  • http://firebirdsql.su/doku.php?id=mon_database via www.translate.ru
  • https://dba.stackexchange.com/questions/29919/firebird-monitoring-tables
  • http://www.upscene.com/documentation/fbtm2/index.html?dm_monitoringtables.htm
  • http://www.firebirdsql.org/file/community/conference-2014/pcisar/#1


回答2:

When SQLConnection.Params property is empty Params.Values['Database'] return empty string even when BeforeConnect or AfterConnect events is fired .

You can use TSQLConnection.OnLogin event. For example :

procedure TForm11.SQLConnection1Login(Database: TSQLConnection;
  LoginParams: TWideStrings);
var
 i : integer;
begin
  //Show all params
  for I := 0 to LoginParams.Count - 1 do
    ShowMessage(LoginParams[i]);

  // Show database
  ShowMessage(LoginParams.Values['Database']);   
end;

Use the OnLogin event to assign values to the User_Name, Password, and Database parameters immediately before TSQLConnection attempts to connect to the database server. OnLogin only occurs if the LoginPrompt property is true. If LoginPrompt is true but there is no OnLogin event handler, a default login dialog appears in which the user can enter a user name and password. The connection fails if correct values for the user name and password are not supplied in the dialog or by the OnLogin event handler.

Source