Monday, May 3, 2010

list of SQL Server Names

How to get the list of SQL Server Names using windows application available in Network

Using SqlDataSourceEnumerator class in the System.Data.Sql namespace, it is easy for developers to enumerate and get a list of existing visible Microsoft SQL Server database instances within a network.

Using the Instance property of the SqlDataSourceEnumerator class you can create an instance of the SqlDataSourceEnumerator object that can be used to retrieve information about available SQL Server instances.
Microsoft SQL Server 2000, SQL Server 2005 and SQL Server 2008 (Katmai) database instances within a network can be listed using this new class "SqlDataSourceEnumerator".
GetDataSources() method of the SqlDataSourceEnumerator class returns information in a DataTable object that can be used to display data about the retrieved SQL database instances on a DataGridView or in a ComboBox object.

Import the Imports System.Data.Sql if it is not imported within the project by default.

Imports System.Data.Sql

Calling the SqlDataSourceEnumerator.Instance will get the public shared instance of SqlDataSourceEnumerator class whose GetDataSources() method will be used to return information about the SQL Servers in a DataTable object.





As you see, DataTable that is returned by the GetDataSources() method of the SqlDataSourceEnumerator class includes the following columns:

ServerName : ServerName is the name of the server where the SQL Server database Instance is running on.

InstanceName : InstanceName is the name of the SQL database instance. If the instance is installed as the Default instance then InstanceName is blank.

IsClustered : IsClustered displays whether the SQL Server Instance is a part of a Cluster SQL Server installation.

Version : Version is the Microsoft SQL Server version number.


Works like this:

using System.Data;
using System.Data.Sql;

SqlDataSourceEnumerator servers = SqlDataSourceEnumerator.Instance;
DataTable serversTable = servers.GetDataSource();

foreach(DataRow row in serversTable.Rows)
{
string serverName = string.format(\"{0}\\\\{1}\", row[0], row[1];
// Add this to your list
}



No comments: