Friday, June 1, 2012

Search for SQL Servers on the network

Two ways to search for available database instances

System.Data.Sql
dtDatabaseServer =
SqlDataSourceEnumerator.Instance.GetDataSources()


Microsoft.SqlServer.Management.Smo
dtDatabaseServer =
SmoApplication.EnumAvailableSqlServers(False)


The second alternative allows you to get the full instance name and if the database server is on the local machine. The only catch is that you have to locate the Microsoft.SqlServer.Smo.dll in the C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies, and copy it to your directory.


Here is the output of each data table:

 System.Data.Sql

Microsoft.SqlServer.Management.Smo