Subscribe to Windows IT Pro
February 05, 2000 12:00 AM

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

Windows IT Pro
InstantDoc ID #14327
Rating: (0)

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 \Binn\Resources\1033\sqldmo.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.


Related Content:

ARTICLE TOOLS

Comments
  • Ben Miller
    8 years ago
    Jul 06, 2004

    This does not work when you are not connected to a network. It won't even list any local servers.

  • Himanshu Saxena
    9 years ago
    Feb 07, 2003

    One of the lines in the Above Code causes "Type mismatch" Error:

    The line is this:
    Set oApplication = New Application


    Solution: The Corrected line is :
    Set oApplication = New SQLDMO.Application

  • Chan
    10 years ago
    Mar 25, 2002

    I am having the same problem as what Paul Carmichael encounter. I put the function into an ActiveX DLL and called from ASP, but the asp page give an "out of memory" error.Anyone out there who can help to resolve this. Thanks

  • Paul Carmichael
    11 years ago
    Aug 20, 2001

    I can't seem to get this to work. I get an error - Not enough storage space is available to complete this operation. MSDN Lib says this error occurs if the ap supports object pooling. Is there a way to disable this in my dll?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.