List All SQL Servers using SQLDMO

When I was still in college, one of my professors (Oracle) in PUP and one of the Systems Analyst of PUPILS asked to list all SQL Servers within LAN as part of their functionality in a system, to manually setup the server and save the settings in an INI file. He then introduced me to the SQLDMO. We're still using VB6.0 then.

The last time I used this library is when I created a DTS job scheduler for the eTouch V2 when I was still working with Soluziona Philippines. That time, it was done and used in ASP .NET.

This is also to answer sir cvega's posted comment to Keith's Article regarding how to programatically list all sql servers in your network. Please see the code below...

C#

private void btnRetrieve_Click(object sender, System.EventArgs e)

{

    SQLDMO.NameList sqlList = null;

    SQLDMO.Application sqlApp = null;

 

    lstServers.Items.Clear();

 

    try

    {

        sqlApp = new SQLDMO.Application();

        sqlList = sqlApp.ListAvailableSQLServers();

 

        for(int ctr = 1; ctr <= sqlList.Count; ctr++)

        {

            lstServers.Items.Add(sqlList.Item(ctr));

        }

    }

    catch(Exception ex)

    {

        MessageBox.Show(ex.Message);

    }

    finally

    {

        if(sqlList != null)

            sqlList = null;

 

        if(sqlApp != null)

            sqlApp = null;

    }

}

VB .NET (Based using code formatter)

Private Sub btnRetrieve_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim sqlList As SQLDMO.NameList = Nothing

        Dim sqlApp As SQLDMO.Application = Nothing

        lstServers.Items.Clear()

        Try

            sqlApp = New SQLDMO.Application

            sqlList = sqlApp.ListAvailableSQLServers

            Dim ctr As Integer = 1

            While ctr <= sqlList.Count

                lstServers.Items.Add(sqlList.Item(ctr))

                System.Math.Min(System.Threading.Interlocked.Increment(ctr), ctr - 1)

            End While

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            If Not (sqlList Is Nothing) Then

                sqlList = Nothing

            End If

            If Not (sqlApp Is Nothing) Then

                sqlApp = Nothing

            End If

        End Try

    End Sub

Attached also is the working sample done using C#. Notes though, SQLDMO should be referenced and the DLL can be found in C:\Program Files\Microsoft SQL Server\80\Tools\Binn.

Attachment: ListSQLServers.zip
Published Friday, June 09, 2006 10:30 AM by MisterClay

Comments

# re: List All SQL Servers using SQLDMO

Thursday, June 08, 2006 7:57 PM by cvega
Thanks, I'll try this later. What I'm currently doing to retrieve Server Instances is using the hard way, scanning the network via TCP/IP manually from port to port, hence not very reliable. By the way, can this code detect multiple instances of SQL Servers on a single machine as well?

# Programmatically List All SQL Servers And Their Databases Using SMO

Thursday, June 08, 2006 9:41 PM by Keith Rull
Basically there are 2 ways to do this. The first one is by using SQL-DMO (Distributed Management Objects) which is a set classic classes that were used to programmatically manipulate and interact with SQL Servers. Please reffer to this article by MisterClay

# re: List All SQL Servers using SQLDMO

Friday, June 09, 2006 2:32 AM by MisterClay
Hmmm... I think when we speak of what framework will be used, SQLDMO and SQLSMO are not always interchangeable. I might be wrong but is SQLSMO applicable only for the .NET framework utilized by .NET 2005 IDE? Because Microsoft.SqlServer is already been integrated and just like working with SQLDMO, it's just the same, except SMO is, speaking of .NET, a managed one. Due to the changes of system environments and as Microsoft slowly shifts its core to mostly managed, there is a big possibility that DMO will not be shifted to a managed one that's why SMO was introduced. Though DMO and SMO can still be used using the latest .NET framework, this is just to clarify (I guess) that, well, using these libraries are framework-dependent. Or, am I being misled by my own conclusions? Hehehe... I'm just wondering if SMO can retrieve information from SQL Server 2000. Have you tried that case?

# re: List All SQL Servers using SQLDMO

Friday, June 09, 2006 10:40 PM by bonskijr
Tanong lang:what's with this in the VB.NET sample? System.Math.Min(System.Threading.Interlocked.Increment(ctr), ctr - 1) Or this is the more safer way of incrementing/decrementing a variable?

# re: List All SQL Servers using SQLDMO

Monday, June 12, 2006 11:25 PM by MisterClay
@sir cvega, haven't tried that one because i don't have more than one sqlserver instance in my workstation here in the office. @sir bonskijr, as noted, I just used the C#-VB.NET converter (http://www.developerfusion.com/utilities/convertcsharptovb.aspx), tinamad ako when I coded that one using C#...

# re: List All SQL Servers using SQLDMO

Monday, June 12, 2006 11:47 PM by MisterClay
Now, I tried to run it again and I observed that I got sql instances regardless if they're on the same workstation or not. I guess pareho lang kung gagawa ko ng another instance sa local ko... http://i11.photobucket.com/albums/a151/misterclay/devpinoy/sql/SQLInstances.jpg

# re: List All SQL Servers using SQLDMO

Tuesday, June 13, 2006 12:10 AM by keithrull
I'm just wondering if SMO can retrieve information from SQL Server 2000. Have you tried that case? <

# re: List All SQL Servers using SQLDMO

Tuesday, June 13, 2006 12:39 AM by cvega
>> are you using WMI to retrieve servers? Nope, just searching each node of the network for access to SQL Server Monitor port (Usually at UDP port 1434, or TCP port 1433), then I'll send a 'list' packet to check for SQL Server instances. Using SQL Server Monitor is really the fastest way to discover instances, but it has a penalty of writing huge amount of codes, which is what I'm trying to avoid. SQLDMO/SQLSMO are doing the same, but it's in a form of easy to implement library.

# Programmatically List All SQL Servers And Their Databases Using SMO

Thursday, September 14, 2006 12:29 PM by Keith Rull
Basically there are 2 ways to do this. The first one is by using SQL-DMO (Distributed Management Objects) which is a set classic classes that were used to programmatically manipulate and interact with SQL Servers. Please reffer to this article by MisterClay

# re: List All SQL Servers using SQLDMO

Monday, July 02, 2007 10:04 PM by Nikhil Badgujar

Excellent...

It could better if you give ASP code also...

# re: List All SQL Servers using SQLDMO

Thursday, September 13, 2007 3:33 PM by Febeskie

Anyone knows what is default the tcp port used SQL SMO?

Thanks,

Febeskie

# re: List All SQL Servers using SQLDMO

Thursday, September 13, 2007 3:46 PM by keithrull

@Febeskie: I think its the same as the SQL Server port which is 1433

# re: List All SQL Servers using SQLDMO

Thursday, September 13, 2007 9:58 PM by bass_player

There's also another way, and that is to read the registry which tells you the instance name, port number, protocols supported, etc.

# re: List All SQL Servers using SQLDMO

Saturday, September 15, 2007 6:25 AM by JCHUF

it gives an error " Fail to get List"

# re: List All SQL Servers using SQLDMO

Friday, October 12, 2007 3:23 AM by MisterClay

@JCHUF, maybe it's time for you to update your server. Try installing SQL SP3.

# re: List All SQL Servers using SQLDMO

Friday, November 23, 2007 3:55 AM by amrut kumar d kanthavadiya

any one can help me?

want to list of all sql server 2000 instatnces on one network

only from sql server 2000 using vb6

# re: List All SQL Servers using SQLDMO

Wednesday, November 28, 2007 1:07 AM by type-x

i tried the compressed file and got nothing

i have my sql express server running and my friends too or it just retrieve the sql server instance not SQLEXPRESS ?

# re: List All SQL Servers using SQLDMO

Sunday, March 16, 2008 3:38 PM by zxevil163

DY9vT0 Hi from Russia!

# re: List All SQL Servers using SQLDMO

Monday, March 17, 2008 11:27 AM by Febeskie

Thanks keithrull

# re: List All SQL Servers using SQLDMO

Monday, March 17, 2008 11:37 AM by febeskie

Question: At what instance do you think the SQLDMO will fail to detect the list of SQL Servers on your network? I'm using SQL 2005 and detection of SQL Servers works for most of the client machines except for one. Firewall is off and I can connect from the client to the SQL server by manually typing in the server\instance name. SQL Authentication is setup as both (Windows and SQL Authentication). I'm running out of ideas. Please help.

Thanks,

Febeskie

# re: List All SQL Servers using SQLDMO

Thursday, March 27, 2008 6:23 PM by zxevil172

lSQe8H      Were a U from?

# re: List All SQL Servers using SQLDMO

Monday, April 14, 2008 2:59 PM by ldlntdpqlv

N4FkBZ  <a href="dpitpzsofmcn.com/.../a>, [url=http://uqbcyimfcizx.com/]uqbcyimfcizx[/url], [link=http://apsnexxvjoho.com/]apsnexxvjoho[/link], http://eirqwjbqhdus.com/

# re: List All SQL Servers using SQLDMO

Monday, June 16, 2008 2:50 AM by tmagalhaes

Hi!

Does anyone has an idea, why using SmoApplication.EnumAvailableSqlServers(false) I can see all the sql servers on my local network, but not their instances?

Thanks,

Tânia.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: