Check if SQL Server is installed on the machine through C #

I am creating an application that is a user interface for accessing two types of databases - SQLite and SQL Server.

The fact is that SQLite does not need to be "installed" from its simply flat database, but, on the other hand, SQL Server (Express / normal) must be installed before use. My question is simple:

Is there a way by which I can find out if an instance of SQL Server was installed on the local computer using a C # program?

+9
c # sql-server sqlite
source share
2 answers

If your application is installed on this computer, you can check the registry using something similar to this:

using Microsoft.Win32; RegistryKey RK = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\MICROSOFT\Microsoft SQL Server"); if(RK != null) { // It there } else { // It not there } 
+10
source share

You have several ways to do this:

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

Direct access is not a recommended MS solution because they can change keys / paths. But other solutions are not reliable and do not provide instances on 64-bit platforms.

Therefore, I prefer to check instances of SQL Server in the registry. Based on this, keep in mind the difference in registry access between the x86 and x64 platforms. Windows 64-bit stores data in different parts of the registry and combines them into views. Therefore, the use of RegistryView is important.

 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 in a 64-bit OS (rather strange, but possible), you will need to look:

 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server 
+17
source share

All Articles