查找由 Inno Setup 的子安装程序安装的程序 (sqlcmd) 的路径

Find path to a program (sqlcmd) installed by child subinstaller of Inno Setup

在我的场景中,我需要 运行 .sql 脚本在我的设置结束时使用 sqlcmd.exe。但是每个版本的 SQL Server 使用不同的路径 sqlcmd.

我正在尝试一些使用 sqlcmd 的解决方案,但 none 有效。 作为最后的测试,我为 运行 我的脚本创建了一个安装程序,但即使这样也不能将 sqlcmd 识别为命令。 有没有办法用函数检索 sqlcmd 路径,以便将值传递给 [Run] 部分? 欢迎提出任何建议。 谢谢

这是我正在尝试的代码

主要installer.exe

[Setup]
PrivilegesRequired=admin

[Files]
; sql server 2019 Express 
Source: "..\PREREQUIREMENTS\SQL\SQLSERVER19\SQLEXPR_x64_ENU.exe"; \
    DestDir: "{tmp}"; DestName: "SQLSERVER2019.exe"; Flags: ignoreversion; \
    MinVersion: 10.0.17134; Tasks: SQL
; sql server 2014 Express 
Source: "..\PREREQUIREMENTS\SQL\SQLSERVER14\SQLEXPR_x64_ENU.exe"; \
    DestDir: "{tmp}"; DestName: "SQLSERVER2014SP3.exe"; Flags: ignoreversion; \
    OnlyBelowVersion: 10.0.17134; Tasks: SQL
; sql management studio 18 
Source: "..\PREREQUIREMENTS\SQL\SSMS\SSMS-Setup-ENU.exe"; DestDir: "{tmp}"; \
    DestName: "SSMS18.exe"; Flags: ignoreversion; Tasks: SSMS
; SQLCMD
Source: "src\DB\ADM_DB_installer.exe"; DestDir: "{tmp}"; Flags: ignoreversion; \
    Components: ADM

ADM_DB_installer.exe

[Setup]
PrivilegesRequired=admin

[Run]
Filename: "sqlcmd.exe"; \
    Parameters: "-v ADMPRIMARY=""{code:GetSQLVar|DBPATH}"" ADMDATA=""{code:GetSQLVar|DBPATH}"" ADMINDEX=""{code:GetSQLVar|DBPATH}"" ADMLOG=""{code:GetSQLVar|DBPATH}"" -S {code:GetSQLVar|SQLSERVER}\{code:GetSQLVar|INSTANCE} -U sa -P {code:GetSQLVar|SAPWD} -i ""{tmp}\CREATE_ADM_DB_1.0.0.sql"" -o ""{code:GetSQLVar|DBPATH}\log.txt"""

可能有更好的解决方案,但您可以做的是从注册表中读取 PATH 并使用 FileSearch 查找程序。

[Run]
Filename: "{code:FileSearchInPath|sqlcmd.exe}"; Parameters: "..."
[Code]
const
  EnvironmentKey = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment';

function FileSearchInPath(FileName: string): string;
var
  Path: string;
begin
  Result := '';
  if not RegQueryStringValue(HKLM, EnvironmentKey, 'PATH', Path) then
  begin
    Log('Cannot read PATH');
  end
    else
  begin
    Log(Format('PATH is %s', [Path]));
    Result := FileSearch(FileName, Path);
    if Result = '' then
    begin
      Log(Format('Cannot find %s', [FileName]));
    end
      else
    begin
      Log(Format('Found %s', [Result]));
    end;
  end;

  if Result = '' then Result := FileName; // let it fail later
end;

以上代码只搜索系统PATH,不搜索用户PATH

您不需要单独的安装程序来执行 sqlcmd.exe