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
Alex klaus
source share