Hi all, today I am going to show a simple code snippets on how to retrieve SQL server name/Instance with machine name using registry.
Step 1: Add reference “ using Microsoft.Win32”.
Step 2: create a form and add controls like label, combo box, button.
Step 3: write code for SQL Instance on button_click event.
- private void button1_Click(object sender, EventArgs e)
- {
- RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
- using(RegistryKey registrtKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
- {
- RegistryKey instanceKey = registrtKey.OpenSubKey(@
- "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
- if (instanceKey != null)
- {
- foreach(var instanceName in instanceKey.GetValueNames())
- {
- cmbSQLInstance.Items.Add(Environment.MachineName + "\\" + instanceName);
- }
- }
- }
- }
Step 4: Execute the program and get the result when click on button.