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

我正在编写一个自定义操作来使用先决条件来检测 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;

        }


冉冉说
浏览 164回答 1
1回答

慕沐林林

最好尽量避免exe自定义操作。可能的解决方案是通过 WiX 查找 sqlcmd:&nbsp;<!-- Find sqlcmd.exe path&nbsp; --><Property Id="SQLBINDIR">&nbsp; <RegistrySearch Id="SqlBinDir11x64"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\ClientSetup"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="Path"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type="raw" Win64="yes" />&nbsp; <RegistrySearch Id="SqlBinDir10x64"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="Path"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type="raw" Win64="yes" />&nbsp; <RegistrySearch Id="SqlBinDir90x64"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="Path"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type="raw" Win64="yes" />&nbsp; <RegistrySearch Id="SqlBinDir11"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\ClientSetup"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="Path"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type="raw" />&nbsp; <RegistrySearch Id="SqlBinDir10"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="Path"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type="raw" />&nbsp; <RegistrySearch Id="SqlBinDir90"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Root="HKLM" Key="SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="Path"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type="raw" /></Property>之后你可以运行它&nbsp;<CustomAction Id="sqlcmd.cmd"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Property="sqlcmd"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value="&quot;[SQLBINDIR]sqlcmd.exe&quot; -E -S $(var.serverinstance) -V 1 -i &quot;$(var.inputfile)&quot; -o &quot;$(var.outputfile)&quot;" />
打开App,查看更多内容
随时随地看视频慕课网APP