Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6206483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:27:28+00:00 2026-05-24T05:27:28+00:00

I use the following code to list all the remote and local SQL Server

  • 0

I use the following code to list all the remote and local SQL Server instances:

public static void LocateSqlInstances()
  {
     using( DataTable sqlSources = SqlDataSourceEnumerator.Instance.GetDataSources())
     {
        foreach(DataRow source in sqlSources.Rows )
        {
           string instanceName = source["InstanceName"].ToString();

           if (!string.IsNullOrEmpty(instanceName))
           {
              Console.WriteLine(" Server Name:{0}", source["ServerName"]);
              Console.WriteLine("   Instance Name:{0}", source["InstanceName"]);
              Console.WriteLine("   Version:{0}", source["Version"]);
              Console.WriteLine();
           }
        }
        Console.ReadKey();
     }
  }

running the code on my local machine. The code can find and list a SQL server express instance (version 9.0.5000) installed but failed to list the other SQL server instance (version 10.0.1600).

I’ve done a lot of research on the Internet and made sure that (1) the Sql Browser is running and (2) the UDP port 1434 is open.

Anybody knows why? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:27:28+00:00Added an answer on May 24, 2026 at 5:27 am

    Thanks a lot to Mitch for the great answer he puts together. However, what I’ve done eventually is like the following:

    I have two separate methods to get local and remote server instance respectively. The local instances are retrieved from the registry. You need to search both WOW64 and WOW3264 hives to get both SQL server 2008 (64bit) and SQL server Express (32 bit)

    here is the code I use:

    /// <summary>
      ///  get local sql server instance names from registry, search both WOW64 and WOW3264 hives
      /// </summary>
      /// <returns>a list of local sql server instance names</returns>
      public static IList<string> GetLocalSqlServerInstanceNames()
      {
         RegistryValueDataReader registryValueDataReader = new RegistryValueDataReader();
    
         string[] instances64Bit = registryValueDataReader.ReadRegistryValueData(RegistryHive.Wow64,
                                                                                 Registry.LocalMachine,
                                                                                 @"SOFTWARE\Microsoft\Microsoft SQL Server",
                                                                                 "InstalledInstances");
    
         string[] instances32Bit = registryValueDataReader.ReadRegistryValueData(RegistryHive.Wow6432,
                                                                                 Registry.LocalMachine,
                                                                                 @"SOFTWARE\Microsoft\Microsoft SQL Server",
                                                                                 "InstalledInstances");
    
         FormatLocalSqlInstanceNames(ref instances64Bit);
         FormatLocalSqlInstanceNames(ref instances32Bit);
    
         IList<string> localInstanceNames = new List<string>(instances64Bit);
    
         localInstanceNames = localInstanceNames.Union(instances32Bit).ToList();
    
         return localInstanceNames;
      }
    

    public enum RegistryHive
    {
      Wow64,
      Wow6432
    }
    
    public class RegistryValueDataReader
    {
      private static readonly int KEY_WOW64_32KEY = 0x200;
      private static readonly int KEY_WOW64_64KEY = 0x100;
    
      private static readonly UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
    
      private static readonly int KEY_QUERY_VALUE = 0x1;
    
      [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]
      static extern int RegOpenKeyEx(
                  UIntPtr hKey,
                  string subKey,
                  uint options,
                  int sam,
                  out IntPtr phkResult);
    
    
      [DllImport("advapi32.dll", SetLastError = true)]
      static extern int RegQueryValueEx(
                  IntPtr hKey,
                  string lpValueName,
                  int lpReserved,
                  out uint lpType,
                  IntPtr lpData,
                  ref uint lpcbData);
    
      private static int GetRegistryHiveKey(RegistryHive registryHive)
      {
         return registryHive == RegistryHive.Wow64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY;
      }
    
      private static UIntPtr GetRegistryKeyUIntPtr(RegistryKey registry)
      {
         if (registry == Registry.LocalMachine)
         {
            return HKEY_LOCAL_MACHINE;
         }
    
         return UIntPtr.Zero;
      }
    
      public string[] ReadRegistryValueData(RegistryHive registryHive, RegistryKey registryKey, string subKey, string valueName)
      {
         string[] instanceNames = new string[0];
    
         int key = GetRegistryHiveKey(registryHive);
         UIntPtr registryKeyUIntPtr = GetRegistryKeyUIntPtr(registryKey);
    
         IntPtr hResult;
    
         int res = RegOpenKeyEx(registryKeyUIntPtr, subKey, 0, KEY_QUERY_VALUE | key, out hResult);
    
         if (res == 0)
         {
            uint type;
            uint dataLen = 0;
    
            RegQueryValueEx(hResult, valueName, 0, out type, IntPtr.Zero, ref dataLen);
    
            byte[] databuff = new byte[dataLen];
            byte[] temp = new byte[dataLen];
    
            List<String> values = new List<string>();
    
            GCHandle handle = GCHandle.Alloc(databuff, GCHandleType.Pinned);
            try
            {
               RegQueryValueEx(hResult, valueName, 0, out type, handle.AddrOfPinnedObject(), ref dataLen);
            }
            finally
            {
               handle.Free();
            }
    
            int i = 0;
            int j = 0;
    
            while (i < databuff.Length)
            {
               if (databuff[i] == '\0')
               {
                  j = 0;
                  string str = Encoding.Default.GetString(temp).Trim('\0');
    
                  if (!string.IsNullOrEmpty(str))
                  {
                     values.Add(str);
                  }
    
                  temp = new byte[dataLen];
               }
               else
               {
                  temp[j++] = databuff[i];
               }
    
               ++i;
            }
    
            instanceNames = new string[values.Count];
            values.CopyTo(instanceNames);
         }
    
         return instanceNames;
      }
    }
    
    
    SqlDataSourceEnumerator.Instance.GetDataSources() is used to get remote sql server instances. 
    

    At the end, I just merge the remote instance list and local instance list to produce the final result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code to create countdowns in Javascript. n is the number
I use the following code to compile a cpp file to object file. g++
I use the following code try to create an array of string vectors, I
I use the following code to layout network drives on a system. I want
I use the following code: Calendar calendar = new GregorianCalendar(0,0,0); calendar.set(Calendar.YEAR, 1942); calendar.set(Calendar.MONTH, 3);
I use the following code to allocate a Console for a WinForm application. The
I use the following code: - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
I Use the following code to load png image: UIImage *imageBack1 = [UIImage imageNamed:@Bar1.png];
I use the following code to load an image into an scroll view. The
If I use the following code I lose the ability to right click on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.