11

I am making an application which is a user interface to access 2 types of databases - SQLite and SQL Server.

The thing is, SQLite doesnt need to be "installed" since its just a flatfile database, but on the other hand, SQL Server (Express/normal) need to be installed before use. My Question is simple:

Is there a way by which i can find out if an instance of SQL Server has been installed on the local machine by using a C# program?

forpas
  • 160,666
  • 10
  • 38
  • 76
Shrayas
  • 6,784
  • 11
  • 37
  • 54
  • 1
    Same question: http://stackoverflow.com/questions/2381055/check-if-sql-server-any-version-is-installed –  Mar 14 '10 at 17:28

3 Answers3

20

You've got several ways to do it:

  • SmoApplication.EnumAvailableSqlServers()
  • SqlDataSourceEnumerator.Instance
  • Direct access to system registry

Direct access isn't the recommended solution by MS, because they can change keys/paths. But the other solutions are not robust and fails providing instances on 64-bit platforms.

Therefore I prefer to check SQL Server instances in System Registry. Doing that, keep in mind the difference in Registry access between x86 and x64 platforms. Windows 64-bit stores data in different parts of system registry and combines them into views. So using RegistryView is essential.

using Microsoft.Win32;

RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
{
    RegistryKey instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
    if (instanceKey != null)
    {
        foreach (var instanceName in instanceKey.GetValueNames())
        {
            Console.WriteLine(Environment.MachineName + @"\" + instanceName);
        }
    }
}

If you are looking for 32-bit instances on a 64-bit OS (pretty weird, but possible), you will need to look:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server
Alex Klaus
  • 8,168
  • 8
  • 71
  • 87
12

If your app is installed on the machine in question, you could inspect the registry using something similar to this:

using Microsoft.Win32; 
RegistryKey RK = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\MICROSOFT\Microsoft SQL Server");
    if(RK != null)
    {
       // It's there 
    }
    else
    {
       // It's not there 
    }
Eternal21
  • 4,190
  • 2
  • 48
  • 63
jbruton
  • 241
  • 2
  • 5
  • 1
    looks good, just add a @ at the start of your OpenSubKey param to allow for the backslashes ;) thanks for the answer! – JM1990 Apr 25 '16 at 08:51
0

The registry can be used to check if SQL server instance is installed. LOCALMACHIN\SOFTWARE\MICROSOFT\Microsoft SQL Server\InstalledInstance

private bool CheckMSSQLInstalled(string instanceName)
{
    RegistryKey RK = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MICROSOFT\Microsoft SQL Server");
    if (RK != null)
    {
        var installedInstance = (string)RK.GetValue("InstalledInstances");
        // installed instance 
        return installedInstance.IndexOf(instanceName) >= 0;
    }
    else
    {
        return false;
    }
}
Mehdi
  • 520
  • 1
  • 5
  • 9