You are currently offline, waiting for your internet to reconnect

How to enumerate available instances of SQL Server by using the SQLDMO components

Extended support for SQL Server 2005 ends on April 12, 2016

If you are still running SQL Server 2005 after April 12, 2016, you will no longer receive security updates and technical support. We recommend upgrading to SQL Server 2014 and Azure SQL Database to achieve breakthrough performance, maintain security and compliance, and optimize your data platform infrastructure. Learn more about the options for upgrading from SQL Server 2005 to a supported version here.

This article was previously published under Q287737
By using SQL Server Distributed Management Objects (SQLDMO), all running SQL Servers on a local network segment can be listed.
To enumerate all available SQL Servers on the local network segment, follow the steps below.

Using Visual Basic

  1. Start a new Standard EXE project.
  2. On the Project menu, click References. Select Microsoft SQLDMO Object library, and then click OK.
  3. Place a CommandButton and a ListBox control on Form1.
  4. Add the following code to the form's module:
    Private Sub Command1_Click()    Dim i As Integer    Dim oNames As SQLDMO.NameList    Dim oSQLApp As SQLDMO.Application    Set oSQLApp = New SQLDMO.Application           Set oNames = oSQLApp.ListAvailableSQLServers()    List1.Clear    For i = 1 To oNames.Count        List1.AddItem oNames.Item(i)    Next iEnd Sub					

Using Visual C++

  1. Start a new, empty Win32 Console Application.
  2. Add a new C++ source file to the project and paste the following code:
    #define UNICODE#define _UNICODE#include <windows.h>#include <tchar.h>#include <stdio.h>#include <initguid.h>#include "C:\PROGRAM FILES\MICROSOFT SQL SERVER\80\TOOLS\DEVTOOLS\INCLUDE\sqldmoid.h"#include "C:\PROGRAM FILES\MICROSOFT SQL SERVER\80\TOOLS\DEVTOOLS\INCLUDE\sqldmo.h"int main(){	LPSQLDMOAPPLICATION pDMOApp = NULL;	LPSQLDMONAMELIST pNameList = NULL;	int	iExitStatus = 1;	// assume failure.	try	{		// Initialize COM.		// 		if FAILED(CoInitialize (NULL))		{			return (iExitStatus);		}		// Create SQLDMOApplication.		if FAILED(CoCreateInstance(CLSID_SQLDMOApplication, NULL, CLSCTX_INPROC_SERVER, IID_IWSQLDMOApplication, (LPVOID*)&pDMOApp))		{			return (iExitStatus);		}			// Get the list of servers.		if SUCCEEDED(pDMOApp->ListAvailableSQLServers(&pNameList))		{			long lCount=0;			BSTR strName=NULL;			//retrieve the count 			pNameList->GetCount(&lCount);			_tprintf(_T("There are %d SQL Servers active on the network.\n"),lCount );			for(long i=0; i < lCount; i++)			{				//get the server name				pNameList->GetItemByOrd(i, &strName);				_tprintf(_T("%s\n"),strName );			}			iExitStatus = 0;		}		if (pDMOApp)		{			pNameList = NULL;			pDMOApp->Release();			pDMOApp = NULL;		}	}	catch(...)	{		_tprintf(TEXT("error dmolistservers unhandled exception (%d)\n"), GetLastError() );	}	return iExitStatus;}					

Article ID: 287737 - Last Review: 02/22/2007 22:15:37 - Revision: 5.3

Microsoft SQL Server 7.0 Standard Edition, Microsoft SQL Server 2000 Standard Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic Enterprise Edition for Windows 6.0, Microsoft Visual C++ 6.0 Enterprise Edition, Microsoft Visual C++ 6.0 Professional Edition, Microsoft Visual C++, 32-bit Learning Edition 6.0, Microsoft SQL Server 2005 Standard Edition, Microsoft SQL Server 2005 Developer Edition, Microsoft SQL Server 2005 Enterprise Edition, Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2005 Workgroup Edition

  • kbhowto kbinfo KB287737
ment.createElement('meta');'ms.dqp0';m.content='true';document.getElementsByTagName('head')[0].appendChild(m);" onload="var m=document.createElement('meta');'ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="">