Determine if SqlLocalDB is installed

2019-07-14 18:24发布

问题:

I am searching for a way to determine in WiX if SQLLocalDB is installed or not. How can I do this? - Can I check a registry-key? - When yes, which key?

回答1:

A RegistrySearch should do it:

<Property Id="LOCALDB">
   <RegistrySearch Id="SearchForLocalDB" Root="HKLM" 
                   Key="SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11E.LOCALDB\MSSQLServer\CurrentVersion"
                   Name="CurrentVersion"
                   Type="raw" />
</Property>

That would get you the version.



回答2:

Checking from the registry may not work all the time becasue if user uninstall the localDb then registry entries may still exist.

Here is the function I am using to identify the localDB installation from command line -

    internal static bool IsLocalDBInstalled()
    {
        // Start the child process.
        Process p = new Process();
        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/C sqllocaldb info";
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        string sOutput = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        //If LocalDb is not installed then it will return that 'sqllocaldb' is not recognized as an internal or external command operable program or batch file.
        if (sOutput == null || sOutput.Trim().Length == 0 || sOutput.Contains("not recognized"))
            return false;
        if (sOutput.ToLower().Contains("mssqllocaldb")) //This is a defualt instance in local DB
            return true;
        return false;
    }