How can I get the name of the database a DBExpress

2019-09-02 18:55发布

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?

2条回答
Summer. ? 凉城
2楼-- · 2019-09-02 19:35

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

查看更多
走好不送
3楼-- · 2019-09-02 19:40

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

查看更多
登录 后发表回答