如何找到用于 FireDac 连接的 MS Access 版本 and/or dll 名称?

How can I find the MS Access version and/or dll name used for a FireDac connection?

我有一个到 Microsoft Access 数据库的 FireDac 连接。我正在像这样在连接上配置 Parms:

Connection.Params.Add('DriverID=MSAcc');
Connection.Params.Add('Database=' + FDatabasePath);
Connection.Connected := true;

总的来说这很好用。但是,在某些情况下,会安装旧的 MS Access 驱动程序。我们想检测这种情况并提醒用户,以便他们可以安装新的驱动程序。

如何获取 FireDac 找到并用于建立连接的驱动程序版本,或者至少是 VendorLib 名称?

我知道我可以在打开我的连接之前在 Phys 连接 Link 上指定一个 VendorLib。我不想那样做。我希望 FireDac 通过它的过程来查找和使用系统上最相关的驱动程序。但是,一旦完成,我想知道它最终使用的是什么驱动程序。

我尝试在连接打开后创建一个 TFDPhysMSAccessDriverLink,希望能够设置 ActualDriverId 或 VendorLib 属性。但是 ActualDriverId 是 MsAcc 而 VendorLib 是空白的。

1。获取驱动程序版本

您可以通过 DriverVersion property, or as a string (which can include also driver description) by the DRIVER_VER property. The queried SQL_DRIVER_VER 信息类型在 ODBC API 中以序号形式获取驱动程序版本,描述如下:

SQL_DRIVER_VER (ODBC 1.0)

A character string with the version of the driver and optionally, a description of the driver. At a minimum, the version is of the form ##.##.####, where the first two digits are the major version, the next two digits are the minor version, and the last four digits are the release version.

使用 FireDAC,您可以通过以下方式获得它:

uses
  FireDAC.Phys.ODBCWrapper;

procedure TForm1.Button1Click(Sender: TObject);
var
  DriverVerStr: string;
  DriverVerInt: TFDVersion;
  ODBCConnection: TODBCConnection;
begin
  ODBCConnection := TObject(FDConnection.CliObj) as TODBCConnection;

  DriverVerStr := ODBCConnection.DRIVER_VER;
  DriverVerInt := ODBCConnection.DriverVersion;

  Memo.Lines.Add(Format('DriverVerStr: %s', [DriverVerStr]));
  Memo.Lines.Add(Format('DriverVerInt: %d', [DriverVerInt]));
end;

2。获取驱动程序支持的 ODBC 版本

要获得所用驱动程序支持的 ODBC 版本,您可以使用 DriverODBCVersion property to obtain ordinal value, or DRIVER_ODBC_VER to get a string value. Queried SQL_DRIVER_ODBC_VER 数据类型信息描述如下:

SQL_DRIVER_ODBC_VER (ODBC 2.0)

A character string with the version of ODBC that the driver supports. The version is of the form ##.##, where the first two digits are the major version and the next two digits are the minor version. SQL_SPEC_MAJOR and SQL_SPEC_MINOR define the major and minor version numbers. For the version of ODBC described in this manual, these are 3 and 0, and the driver should return "03.00".

使用 FireDAC,您可以通过以下方式获得它:

uses
  FireDAC.Phys.ODBCWrapper;

procedure TForm1.Button1Click(Sender: TObject);
var
  ODBCVerStr: string;
  ODBCVerInt: TFDVersion;
  ODBCConnection: TODBCConnection;
begin
  ODBCConnection := TObject(FDConnection.CliObj) as TODBCConnection;

  ODBCVerStr := ODBCConnection.DRIVER_ODBC_VER;
  ODBCVerInt := ODBCConnection.DriverODBCVersion;

  Memo.Lines.Add(Format('ODBCVerStr: %s', [ODBCVerStr]));
  Memo.Lines.Add(Format('ODBCVerInt: %d', [ODBCVerInt]));
end;

3。获取驱动程序访问的 DBMS 产品的版本

您可以通过DBMS_VER property. ODBC API describes the queried SQL_DBMS_VER信息类型获取访问的DBMS产品版本为:

SQL_DBMS_VER (ODBC 1.0)

A character string that indicates the version of the DBMS product accessed by the driver. The version is of the form ##.##.####, where the first two digits are the major version, the next two digits are the minor version, and the last four digits are the release version. The driver must render the DBMS product version in this form but can also append the DBMS product-specific version. For example, "04.01.0000 Rdb 4.1".

使用 FireDAC 可以这样获取(没有 属性 返回解析后的序号版本号,所以让我们尝试自己解析为序号值):

uses
  FireDAC.Stan.Util, FireDAC.Phys.ODBCWrapper;

procedure TForm1.Button1Click(Sender: TObject);
var
  DBMSVerStr: string;
  DBMSVerInt: TFDVersion;
  ODBCConnection: TODBCConnection;
begin
  ODBCConnection := TObject(FDConnection.CliObj) as TODBCConnection;

  DBMSVerStr := ODBCConnection.DBMS_VER;
  DBMSVerInt := FDVerStr2Int(DBMSVerStr);

  Memo.Lines.Add(Format('DBMSVerStr: %s', [DBMSVerStr]));
  Memo.Lines.Add(Format('DBMSVerInt: %d', [DBMSVerInt]));
end;

4。获取FireDAC统一版本信息

FireDAC提供连接对象的ClientVersion and ServerVersion properties which are accessible through the ConnectionMetaDataIntf接口属性。

对于 ODBC 驱动程序,ClientVersion property returns the driver version (described in section 1 here) if the driver is not a single tier driver, DBMS product version otherwise (described in section 3). The ServerVersion returns 始终是 ODBC 驱动程序的 DBMS 产品版本(在此处第 3 节中描述)。

例如:

procedure TForm1.Button1Click(Sender: TObject);
var
  Metadata: IFDPhysConnectionMetadata;
begin
  Metadata := FDConnection.ConnectionMetaDataIntf;
  try
    Memo.Lines.Add(Format('ClientVersion: %d', [Metadata.ClientVersion]));
    Memo.Lines.Add(Format('ServerVersion: %d', [Metadata.ServerVersion]));
  finally
    Metadata := nil;
  end;
end;