【问题标题】:FireDAC ResultConnectionDef and information about Server and PortFireDAC ResultConnectionDef 以及有关服务器和端口的信息
【发布时间】:2018-08-20 15:21:09
【问题描述】:

DelphiXE 10.2.2

在这里检查 old http://codeverge.com topic 那时在哪里工作,但现在是

ResultConnectionDef 用于获取有关已建立连接(服务器和端口)的信息。

ZeosLib (ZeosDBO) code:

procedure TMainForm.UpdateCaption;
begin
  Caption := Format('Truice %s - Connection: %s:%d / %s', [VERSION_EXE, MyTrinityConnection.HostName, MyTrinityConnection.Port, GetDBVersion]);
  Application.Title := Caption;
end;

使用 FireDac:

uses FireDAC.Phys.MySQLDef, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQL, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, FireDAC.Comp.Client, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Script, FireDAC.Comp.ScriptCommands, FireDAC.Stan.Util; 

procedure TMainForm.UpdateCaption;
begin
  Caption := Format('Truice %s - Connection: %s:%d / %s', [VERSION_EXE, MyTrinityConnection.ResultConnectionDef.Server, MyTrinityConnection.ResultConnectionDef.Port, GetDBVersion]);
  Application.Title := Caption;
end;

结果:

'IFDStanConnectionDef' does not contain a member named 'Server'
'IFDStanConnectionDef' does not contain a member named 'Port'

问题:

  1. FireDAC 是否对这部分进行了更改?
  2. 收集服务器和端口以进行活动连接的最佳方法是什么?



维多利亚解决方案后的最终代码,如下所示:

procedure TMainForm.UpdateCaption;
var
  Server: string;
  Port: Integer;
begin
  Server := TFDPhysMySQLConnectionDefParams(MyTrinityConnection.ResultConnectionDef.Params).Server;
  Port := TFDPhysMySQLConnectionDefParams(MyTrinityConnection.ResultConnectionDef.Params).Port;
  Caption := Format('Truice %s - Connection: %s:%d / %s', [VERSION_EXE, Server, Port, GetDBVersion]);

  Application.Title := Caption;
end;

【问题讨论】:

    标签: delphi firedac


    【解决方案1】:

    1。 FireDAC 是否在这部分进行了更改?

    是的,它已经改变了。底层接口 (IADStanConnectionDef) 的旧变体具有 ServerPort 等属性。这个界面的新版本 (IFDStanConnectionDef) 发布 Params 属性,该属性指向特定的 DBMS 参数集合实现。

    更改的原因可能是受支持的 DBMS 的多样性(其中一些没有远程连接)。

    2。如何获取活动 MySQL 连接的服务器和端口?

    据我所知,至少有两种方法可以获取活动 MySQL 连接的服务器和端口。第一个是使用提到的ResultConnectionDef 及其 Params 属性(两个示例中的 FDConnection1 对象目前都假定连接到 MySQL 服务器),例如:

    uses
      FireDAC.Phys.MySQLDef;
    
    var
      Port: Integer;
      Server: string;
    begin
      Port := TFDPhysMySQLConnectionDefParams(FDConnection1.ResultConnectionDef.Params).Port;
      Server := TFDPhysMySQLConnectionDefParams(FDConnection1.ResultConnectionDef.Params).Server;
      ...
    end;
    

    MySQL 的另一个选择是从TMySQLSession 会话对象获取这些信息,例如:

    uses
      FireDAC.Phys.MySQLWrapper;
    
    var
      Port: Cardinal;
      Server: string;
    begin
      Port := TMySQLSession(FDConnection1.ConnectionIntf.CliObj).Port;
      Server := TMySQLSession(FDConnection1.ConnectionIntf.CliObj).Host;
      ...
    end;
    

    上述两种方式相同(此时),因为TMySQLSession 在内部从连接定义参数接收主机和端口(这意味着您设置的就是您得到的,就像 ZEOS 使用它们的属性一样)。

    如果您想获得 MySQL 报告的主机和端口信息,您可以获取 hostnameport 系统变量的值。

    【讨论】:

    • 提醒一下:“Params.Values['Port'] 和 ['Hostname'] 为空,默认端口!即对于 Mysql,如果设置了 3306 端口,Params Port 值将不存在,但它如果您将端口更改为例如 3310,则会出现
    • @Vancalar,这就是我所说的“你所设置的就是你得到的”。
    猜你喜欢
    • 2016-02-09
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多