You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

从Advantage Native Delphi组件迁移至FireDAC:连接类型获取方法咨询

FireDAC中判断服务器连接方式(对应TAdsConnection.ConnectionType)

嗨,我来帮你梳理下从Advantage Native Delphi组件迁移到FireDAC时,怎么对应原来的TAdsConnection.ConnectionType功能来确定服务器连接类型~

FireDAC并没有像Advantage那样提供直接的ConnectionType属性,但我们可以通过解析FireDAC连接组件的Params参数,来判断对应的连接类型(Local、Remote、AIS(Internet)),具体判断逻辑如下:

  • Local本地连接:如果是连接本地Advantage数据库,FDConnection.Params里的Database会指向本地文件路径,且Server参数为空、或设为localhost/127.0.0.1,同时没有配置AIS相关的协议参数。你可以通过这几个条件组合判断。
  • Remote远程连接:当连接局域网内的Advantage服务器时,FDConnection.Params.Values['Server']会设为远程服务器的IP或主机名,且使用Advantage默认的6262端口,没有HTTP/HTTPS协议配置,这就对应原来的Remote类型。
  • AIS(Internet)连接:通过Advantage Internet Server连接时,FDConnection.Params里会明确设置ProtocolHTTPHTTPSServer为公网域名/IP,同时端口通常设为80(HTTP)或443(HTTPS),通过协议参数就能直接判断这种类型。

给你一个简单的Delphi代码示例,用来快速判断当前连接类型:

function GetFDConnectionType(AFDConnection: TFDConnection): string;
var
  sServer, sProtocol: string;
begin
  sServer := AFDConnection.Params.Values['Server'];
  sProtocol := AFDConnection.Params.Values['Protocol'];

  if SameText(sProtocol, 'HTTP') or SameText(sProtocol, 'HTTPS') then
    Result := 'AIS(Internet)'
  else if (sServer = '') or SameText(sServer, 'localhost') or SameText(sServer, '127.0.0.1') then
    Result := 'Local'
  else
    Result := 'Remote';
end;

需要注意的是,不同版本的FireDAC对Advantage的参数支持可能略有差异,你可以结合自己实际的连接配置微调判断逻辑,确保和原来的TAdsConnection.ConnectionType行为一致。


内容的提问来源于stack exchange,提问作者user3396999

火山引擎 最新活动