473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Listing available SQL Servers in a network/pc

Hi There ,


I wanted to know if there's an easy way to list all available SQL Servers Names in my network/pc from a .net C# application ?

Thanks in advance.
Jan 6 '08 #1
3 3841
camel
55
Use SQL Server Management Objects (or SQLDMO if pre SQL 2005), here is method that uses SMO:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         public static string[] EnumerateServers()
  3.         {
  4.             ArrayList colServers = new ArrayList();
  5.             string[] arrServers = null;
  6.  
  7.             // Servers registered in SQL Management Studio
  8.             Microsoft.SqlServer.Management.Smo.RegisteredServers.RegisteredServer[] rsvrs = SmoApplication.SqlServerRegistrations.EnumRegisteredServers();
  9.             if (rsvrs != null)
  10.             {
  11.                 foreach (Microsoft.SqlServer.Management.Smo.RegisteredServers.RegisteredServer rs in rsvrs)
  12.                 {
  13.                     String name = "";
  14.  
  15.                     name = rs.Name.Replace(".", System.Environment.MachineName)
  16.                                         .Replace("(local)", System.Environment.MachineName)
  17.                                         .Replace("localhost", System.Environment.MachineName);
  18.  
  19.                     if (name.Length > 0 && colServers.IndexOf(name.ToUpper()) < 0)
  20.                     {
  21.                         colServers.Add(name.ToUpper());
  22.                     }
  23.                 }
  24.             }
  25.  
  26.             // Auto-Detect Network Servers
  27.             DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
  28.             if (dt.Rows.Count > 0)
  29.             {
  30.                 foreach (DataRow dr in dt.Rows)
  31.                 {
  32.                     if (colServers.IndexOf(dr["Name"].ToString().ToUpper()) < 0)
  33.                     {
  34.                         colServers.Add(dr["Name"].ToString().ToUpper());
  35.                     }
  36.                 }
  37.             }
  38.  
  39.  
  40.             // Local Installations (if machine call is running on is a SQL Server)
  41.             Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
  42.             if (rk != null)
  43.             {
  44.                 String[] instances = (String[])rk.GetValue("InstalledInstances");
  45.                 if (instances!=null && instances.Length > 0)
  46.                 {
  47.                     foreach (String element in instances)
  48.                     {
  49.                         String name = "";
  50.                         //only add if it doesn't exist
  51.                         if (element == "MSSQLSERVER")
  52.                             name = System.Environment.MachineName;
  53.                         else
  54.                             name = System.Environment.MachineName + @"\" + element;
  55.  
  56.                         if (colServers.IndexOf(name.ToUpper()) < 0)
  57.                         {
  58.                             colServers.Add(name.ToUpper());
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             if (colServers.Count > 0)
  65.             {
  66.                 arrServers = (string[])colServers.ToArray(typeof(string));
  67.             }
  68.  
  69.             return arrServers;
  70.         }
  71.  
  72.  
  73.  
Jan 6 '08 #2
hello good day... i'm new here.. i wanted to post a new topic but there was a similar topic but it new posts were no longer allowed.. and this was the nearest topic i could find... SQL.DMO does the work... in fact it gets the job done fast and easy... the problem is.. it doesn't work when there is a firewall.. and I'm using C# and when i try to get a list of servers running all i get is a blank on the combo box...and the other guys just wouldn't turn off the firewall because they think it is necessary to have one.. so i'm the one who's supposed to adjust I have successfully got a list of servers using the system registry by extracting the data from the Keys by using Microsoft.Win32.. but sometimes funny things happen when there are new servers.. We were hoping to able to get the list of servers within the network but because this is the only method I know that works even when the firewall was turned on.. then I guess I'll have to settle with it.. unless of course anybody out there has a better suggestion... anyone?
Mar 24 '08 #3
what this:ArrayList colServers = new ArrayList();?
what mean ArrayList?
Mar 16 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Job Lot | last post by:
Using VB.NET how can I find all available SQL Server on a local network and list all the databases in each server. Thanks
3
by: John Barr | last post by:
Does anyone know how to search for Instances of SQL Server running on your network through code to populate a drop down like is done in the SQL Server Query Analyzer, and then once an instance is...
1
by: Jerry Negrelli | last post by:
Can anyone help translate this into C#??? It's supposed to list all subnets in all AD sites (see URL below). I placed it in a VB class library (one class with one "Function", WScript.Echo...
5
by: Naveen Mukkelli | last post by:
Hi, I've got two network adaptors in my computer(Windows server 2003). One for public network such as internet, and the other for a private network. I mean, I want to connect other 2 PCs (XP...
2
by: DKC | last post by:
Hi All, Is there any way wherein I can get a list of database servers on my local network. Any help appreciated. I am using VB.NET 2003 Thanks DKC
1
by: Roger | last post by:
I would like to get a list of machines on my network (Workstations and/or Servers). Is there a way to do this in VB.Net? Thanks, Rog
5
by: Steve | last post by:
I would like to provide users of a client program a list of databases on the network that match a certain pattern, like "%frp%". I have seen a list of databases in the Windows XP ODBC configuration...
0
by: mandarkraftware | last post by:
Hi All, Let me present you with list of consultants currently available. Name / Job Title Summary Ramchandran/Java · 7+ years experience in the field of Software Development. · Expertise...
1
by: Sachin | last post by:
Hi All, Let me present you with list of consultants currently available. Name / Job Title Summary Ramchandran/Java · 7+ years experience in the field of Software Development. · Expertise...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.