3

Possible Duplicate:
How to list available instances of SQL Servers using SMO in C#?

I'm writing a quick application which is to present the user with a list of server names/instances he or she may choose from to connect to an SQL Server database. Basically I am trying to populate these options into a combobox, which should have options similar to these:

  • (local)
  • Joe-PC\SQLEXPRESS
  • etc.

I have successfully obtained the databases corresponding to a server, and their respective tables, through ADO.NET. However, I cannot find any code samples which allow me to retrieve the above data.

Community
  • 1
  • 1
Dot NET
  • 4,891
  • 13
  • 55
  • 98

3 Answers3

5

You can use SMO :

- Just add Microsoft.SqlServer.Smo.dll assembly (You can find it in Program Files\Microsoft SQL Server\100\SDK\Assemblies SQL SERVER 2008) to your project resources.
- You can always refer to MSDN.
- Here is the function you need :

SmoApplication.EnumAvailableSqlServers();
HichemSeeSharp
  • 3,240
  • 2
  • 22
  • 44
1

One way to do this is to use the SQL Browser Service. You can use the SqlDataSourceEnumerator to enumerate all SQL server instances that are have their SQL Browser service running.

The GetDataSources method will return a DataTable of available instances. The MSDN Documentation provides a code example, too.

This is the same mechanism that SQL Server Management Studio will use to populate its server drop down on the connection dialog.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
-1

The dirty way to do it, is to have a service that tries to connect to all the servers. If the connection fails, you know the server is offline and not to publish that server as a viable option.

Matt Westlake
  • 3,499
  • 7
  • 39
  • 80