Monday, June 4, 2007

Enumerating SQL Instances in a local network

When I started working on a versioning tool for SQL server databases, I wanted to list all the SQL server instances in the local network. After much searching and testing, I came up with this code snippet below. Apparently the .Net framework already has a class that can do the job for you. There are various ways on doing these and if you search with the title of the blog on google you will definitly get quite a few.I will keep it simple and small.For more information search on the classname SqlDataSourceEnumerator in msdn.


try
{
SqlDataSourceEnumerator Sqlen = SqlDataSourceEnumerator.Instance;
DataTable dt = Sqlen.GetDataSources();
foreach (DataRow row in dt.Rows)
{
ServercomboBox.Items.Add(row[0]);
}
}
catch
{
MessageBox.Show("Server lists could not be loaded");
}

Hope that helps

No comments: