Check if SQL Server is installed C #

1

I am finalizing a C # application that contains a database in SQL Server. How can I tell if the user has the SQL Server 2012 Express Local DB installed?

Can you check via registry on x86 and x64 ?

Basically it would alert the user if SQL Server 2012 Express Local DB was not installed. This is because the installer I am using to setup the app does not contain prerequisites for SQL Server 2012 Express Local DB .

    
asked by anonymous 13.02.2016 / 04:07

2 answers

4

It stays in this registry path, just like all SQL versions:

Usethefollowingcodetocheckiftheregistryexists:

RegistryKeykey=Registry.LocalMachine.OpenSubKey("Software\Microsoft\Microsoft SQL Server Local DB\Installed Versions");
if(key != null && key.GetSubKeyNames().Count() > 0)
{
    //existe!
}

That's because it might be version 11 or 12 ... etc.

    
13.02.2016 / 09:29
3

One way to check whether SQL Server Local DB is installed is through HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions already quoted in @daniloloko's response, however, you need to specify for RegistryKey whether the system is x86 or x64 .

We can do this through the Is64BitOperatingSystem property of the class Environment to determine whether the system is 64bit or 32bit, then specify which record view should be used RegistryView.Registry64; or RegistryView.Registry32; on the RegistryView object.

Follow the software that verifies that SQL Server Local BD is installed:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace SQLServerLocalDBInstalledExemplo
{
    class Program
    {
        static void Main(string[] args)
        {
            RegistryView registryViewArchitecture;

            if (Environment.Is64BitOperatingSystem)
                registryViewArchitecture = RegistryView.Registry64;
            else
                registryViewArchitecture = RegistryView.Registry32;

            using (RegistryKey registryKeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryViewArchitecture)) 
            {
                RegistryKey registryKey = registryKeyLocalMachine.OpenSubKey(@"HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions", false);

                if (registryKey != null)
                    Console.WriteLine("SQL Server Local DB instalado.");
                else
                    Console.WriteLine("SQL Server Local DB não esta instalado.");
            }

            Console.ReadKey();
        }
    }
}

To manipulate the registry, you must add the namespace. Win32 , also see that it was necessary to use the OpenBaseKey method of the RegistryKey class, this method allows you to open a new RegistryKey representing the requested key on the local machine with the specified view in the registryViewArchitecture object.

Sources:
Check if SQL Server is installed on a machine through C #
SQL2012 LocalDB: how to check in c # if it is currently installed?

    
13.02.2016 / 19:38