How can I list all available SQL Servers in my application?

Neil Pike

February 4, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. Two methods :-

1. The ISQL and OSQL commands have a -L option to list servers. This can then be run from xp_cmdshell.

Exec master..xp_cmdshell 'ISQL -L'
Exec master..xp_cmdshell 'OSQL -L'


2. Use SQL-DMO. The method/api is ListAvailableSQLServers. This returns a NameList object enumerating network-visible SQL Servers.

Details on DMO are provided in the Books-Online and/or a separate help file. These are installed as part of any SQL Server install.

A detailed example is below :-

To enumerate all network-visible SQL servers using SQL-DMO objects, create a new standard EXE project and add a reference to sqldmo.rll. This file can be found in BinnResources1033sqldmo.rll under the SqlServer70
directory. 

Now add this code and declaration in your form's code:

Private Function GetAllSqlServerCollection(colSqlServers As Collection)
As
Boolean
Dim intIndex As Integer
Dim oApplication As SQLDMO.Application
Dim oNameList As SQLDMO.NameList

Set oApplication = New Application
With oApplication
Set oNameList = .ListAvailableSQLServers
With oNameList
For intIndex = 1 To .Count
colSqlServers.Add (oNameList.Item(intIndex))
Next
End With
End With
Set oApplication = Nothing
GetAllSqlServerCollection = True
End Function

This code quickly fetches a list of SQL servers and can be put inside a combo box's drop-down event to always get a refreshed list of SQL servers on your form.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like