Enumerating SQL Server Instances

No.of Views1472
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  16 Feb 2010 02:02:02
Tag : CSharp , How to
Learn how to enumerate currently installed SQL Server instances on this machine and on the network.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Background

Starting from version 2.0, .NET supports a mechanism to enumerate the SQL Server instances in the local network. This is done by System.Sql.SqlDataSourceEnumerator class that resides on the assembly System.Data.

This class is a implements the singleton pattern, means that you can not instantiate it, and only a single instances serves all, accessed via the static property Instance.

Using the GetDataSources method of SqlDataSourceEnumerator, you get a DataTable object that contains four columns, ServerName, InstanceName, IsCulstered, and Version (Clear names, right?).

Implementation

The following code retrieves information about all visible SQL Server instances in the local network:

static void Main()
{
DataTable table = SqlDataSourceEnumerator.Instance.GetDataSources();
DisplayTable(table);
}

private static void DisplayTable(DataTable table)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
Console.WriteLine(new string('-', 30));
}
}
If you have a firewall installed on your machine, you will be asked to give permissions to the application.
GetDataSources() demands the FullTrust permission set.
Due to the nature of the mechanism used by SqlDataSourceEnumerator to locate data sources on a network, the method will not always return a complete list of the available servers, and the list might not be the same on every call.
 
Sign Up to vote for this article
 
About Author
 
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Location-Egypt
Joined date-30 Jul 2009
Home Page-http://WithDotNet.net
Blog Page-http://JustLikeAMagic.com
Independent software developer, trainer, and technical writer from Egypt born in 1991
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top