Check that SQL Server is installed C#

Asked

Viewed 1,560 times

1

I’m finishing a C# application that contains a database in SQL Server. How do I know if the user has the SQL Server 2012 Express Local DB installed?

It is possible to check via registry in x86 and x64?

Basically it would be to alert the user if the 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.

  • Try to check through this record: HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions\11.0

  • The answer to your question has already been given , but if it is possible to migrate to localdb 2016 you do not need anything installed to run !

  • That is, there is no need to install SQL Server for anyone who runs and is not installed?

2 answers

4

It stays in this log path, as well as all SQL verses:

inserir a descrição da imagem aqui

Use the following code to check if the record exists:

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

That’s because it can be version 11 or 12... etc.

  • But with this you will not check for x86 and x64, correct?

  • See @Dener Carvalho’s reply for more details on this.

4

One way to check whether SQL Server Local DB is installed is through the record HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions already quoted in @daniloloko’s reply, however, it is necessary to specify for the RegistryKey if the system is x86 or x64.

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

Follow the software that checks whether 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 record you need to add the namespace Microsoft.Win32, see also that it was necessary to use the method OpenBaseKey class RegistryKey, this method allows you to open a new RegistryKey representing the requested key on the local machine with the display mode specified on the object registryViewArchitecture.

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

  • Thank you for the excellent and detailed solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.