How can I get the name of the database a DBExpress TSqlConnection is actually connected to?

徘徊边缘 提交于 2019-12-11 18:38:00

问题


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



来源:https://stackoverflow.com/questions/33939006/how-can-i-get-the-name-of-the-database-a-dbexpress-tsqlconnection-is-actually-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!