Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list the SQL Server Instances installed on a local machine? ( Only local )

Tags:

I would like to know if there's a way to list the SQL Server instances installed on the local computer.

SqlDataSourceEnumerator and EnumAvailableSqlServers don't do the trick as I don't need the instances that are over the local network.

like image 201
Alexis Kalyvitis Avatar asked Mar 14 '11 15:03

Alexis Kalyvitis


People also ask

How do I get a list of SQL Server instances installed on a machine?

Get-ChildItem cmdlet To list all the SQL instances, navigate to the root directory and run the Get-Childitem to get the list. Note: The path SQLSERVER:\SQL\<ServerName> has all the installed SQL instances of the listed server.

How do I see all SQL Server instances?

Go to Start > Programs > Microsoft SQL Server > Configuration Tools. Locate the running MS SQL Server instance name (circled below in red).

Where are SQL databases stored locally?

The system database files for the database are stored in the local AppData path, which is normally hidden. For example, C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1\ .


2 Answers

Direct access to Windows Registry isn't the recommended solution by MS, because they can change keys/paths. But I agree that SmoApplication.EnumAvailableSqlServers() and SqlDataSourceEnumerator.Instance fails providing instances on 64-bit platforms.

Getting data from Windows Registry, keep in mind the difference in Registry access between x86 and x64 platforms. 64-bit edition of Windows 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 
like image 170
Alex Klaus Avatar answered Oct 15 '22 20:10

Alex Klaus


You could call EnumAvailableSQlServers with a localOnly = True

public static DataTable EnumAvailableSqlServers(bool localOnly) 

See MSDN docs for EnumAvailableSqlServers

like image 39
marc_s Avatar answered Oct 15 '22 20:10

marc_s