在机器中检测 SQLCMD 运行 的正确方法 - WIX 安装程序先决条件

Proper way to detect SQLCMD running in the machine - WIX installer Pre-requisite

我正在编写一个自定义操作以使用先决条件来检测 SQLCMD 是否正在机器上运行。我可以使用下面的代码检查机器上是否有 SQLCMD。

在安装程序中,我们使用 CMDPrompt 来处理 SQLCMD,但是如果机器在环境变量中没有 SQLCMD 路径,它将无法工作。但我也想知道为什么 CMDprompt 给出 'sqlcmd' is not recognized as an internal or external command error.

[CustomAction]
        public static ActionResult FindSqlCMD(Session session)
        {
            DebugMsg(session, "Start FindSqlCMD");
            string[] sqlVersions = session["SQLVERSIONS"].Split(';');

            List<RegistryKey> sqlKeys = new List<RegistryKey>();

            var sqlDacPaths = new string[] { "C:\Program Files (x86)\Microsoft SQL Server\{0}\DAC\bin", "C:\Program Files\Microsoft SQL Server\{0}\DAC\bin" };

            var sqlPackageName = "SqlPackage.exe";

            foreach (string SqlVersion in sqlVersions)
            {


                foreach (var sqlDacPath in sqlDacPaths)
                {
                    var path = string.Format(sqlDacPath, SqlVersion);



                    if (Directory.Exists(path))
                    {

                        var sqlPackagePath = Path.Combine(path, sqlPackageName);

                        if (File.Exists(sqlPackagePath))
                        {


                            session["SQLBINDIR"] = sqlPackagePath;
                            return ActionResult.Success;
                        }
                    }
                }
            }

            DebugMsg(session, string.Format("Didn't find any SQL DAC SQLPackage"));

            session.Log("End FindSqlCMD");
            return ActionResult.Success;
        }

最好尽量避免 exe 自定义操作。可能的解决方案是通过 WiX 找到 sqlcmd:

 <!-- Find sqlcmd.exe path  -->
<Property Id="SQLBINDIR">
  <RegistrySearch Id="SqlBinDir11x64" 
                  Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server0\Tools\ClientSetup" 
                  Name="Path" 
                  Type="raw" Win64="yes" />
  <RegistrySearch Id="SqlBinDir10x64" 
                  Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server0\Tools\ClientSetup" 
                  Name="Path" 
                  Type="raw" Win64="yes" />
  <RegistrySearch Id="SqlBinDir90x64" 
                  Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\Tools\ClientSetup" 
                  Name="Path" 
                  Type="raw" Win64="yes" />
  <RegistrySearch Id="SqlBinDir11" 
                  Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server0\Tools\ClientSetup" 
                  Name="Path" 
                  Type="raw" />
  <RegistrySearch Id="SqlBinDir10" 
                  Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server0\Tools\ClientSetup" 
                  Name="Path" 
                  Type="raw" />
  <RegistrySearch Id="SqlBinDir90" 
                  Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\Tools\ClientSetup" 
                  Name="Path" 
                  Type="raw" />

</Property>

之后你可以运行它

 <CustomAction Id="sqlcmd.cmd" 
              Property="sqlcmd" 
              Value="&quot;[SQLBINDIR]sqlcmd.exe&quot; -E -S $(var.serverinstance) -V 1 -i &quot;$(var.inputfile)&quot; -o &quot;$(var.outputfile)&quot;" />