c pound

I reject your reality and substitute my own!
posts - 46, comments - 46, trackbacks - 40

My Links

News

Archives

Image Galleries

Blog Communities

Blog is a stupid word

Lunch Hour

Resources

Find SQL Server instances on your network

Previously, the discovery of what SQL servers exist on your network was quite a task. The general approach involved PInvoke. Yeah. Now, it .Net 2, it's a one-liner. Sweet!

public List<SQLServer> GetServers()
{
    DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();

    List<SQLServer> servers = new List<SQLServer>();
    foreach(DataRow row in dt.Rows)
    {
        SQLServer svr = new SQLServer();
        svr.Name = row.ItemArray[
0] as string;
        svr.InstanceName = row.ItemArray[
1] as string;
        svr.IsClustered = row.ItemArray[
2] as string;
        svr.Version = row.ItemArray[
3] as string;
        servers.Add(svr);
    }
    return servers;
}

Hmm... I just noticed that they used a singleton with one method instead of just making GetDataSources static. Odd.

Print | posted on Monday, March 27, 2006 1:58 PM |

Feedback

Gravatar

# re: Find SQL Server instances on your network

Hi,
I'm not finding SQLServer as a type or namespace. Can you post where it is to be found. Thanks.
John
4/30/2006 6:59 AM | John R
Gravatar

# re: Find SQL Server instances on your network

Sorry John,
I should have mentioned that SQLServer is just a simple class I created to hold the server information and publish a ToString function so I could just drop it into a list-box.

public class SQLServer
{
public string Name;
public string InstanceName;
public string IsClustered;
public string Version;

public override string ToString()
{
return Name;
}
}

I removed the comments from the above code, so look here for a description of the fields : http://msdn2.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator.getdatasources.aspx
5/1/2006 5:49 AM | Dan Koster
Gravatar

# re: Find SQL Server instances on your network

Hi,

I am on a project using Mono 2.0, and SqlDataSourceEnumerator.Instance is not implemented as yet in Mono 2.0. Is there any other way of finding the instances of the servers on the network?

Thanks.

Regards
Rupesh.
12/9/2008 1:17 AM | Rupesh
Gravatar

# re: Find SQL Server instances on your network

I used this code and it works, but only seems to gather details on the first 20 or so servers in the enumeration list. I've got about 50 servers in the list, so I added another step.

If the extended properties come back DBNull, I attempt to log in to the server and execute a "SELECT @@VERSION" to get some more details. That way, I can get details on everything I'm able to log into.

Thanks for the code - very helpful!
12/17/2009 3:44 PM | Ryan McCauley
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: