Microsoft KB Archive/287737

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

Article ID: 287737

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft SQL Server 7.0 Standard Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * 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

-



This article was previously published under Q287737



SUMMARY
By using SQL Server Distributed Management Objects (SQLDMO), all running SQL Servers on a local network segment can be listed.



MORE INFORMATION
To enumerate all available SQL Servers on the local network segment, follow the steps below.

Using Visual Basic
 Start a new Standard EXE project. On the Project menu, click References. Select Microsoft SQLDMO Object library, and then click OK. Place a CommandButton and a ListBox control on Form1.  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 i End Sub 

Using Visual C++
 Start a new, empty Win32 Console Application.  Add a new C++ source file to the project and paste the following code:
 * 1) define UNICODE
 * 2) define _UNICODE


 * 1) include <windows.h>
 * 2) include <tchar.h>
 * 3) include <stdio.h>
 * 4) include <initguid.h>


 * 1) include &quot;C:\PROGRAM FILES\MICROSOFT SQL SERVER\80\TOOLS\DEVTOOLS\INCLUDE\sqldmoid.h&quot;
 * 2) include &quot;C:\PROGRAM FILES\MICROSOFT SQL SERVER\80\TOOLS\DEVTOOLS\INCLUDE\sqldmo.h&quot;

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(&quot;There are %d SQL Servers active on the network.\n&quot;),lCount );

for(long i=0; i < lCount; i++) {               //get the server name pNameList->GetItemByOrd(i, &strName); _tprintf(_T(&quot;%s\n&quot;),strName ); }           iExitStatus = 0; }

if (pDMOApp) {           pNameList = NULL; pDMOApp->Release; pDMOApp = NULL; }   }    catch(...) {       _tprintf(TEXT(&quot;error dmolistservers unhandled exception (%d)\n&quot;), GetLastError ); }

return iExitStatus; }                   </li></ol>

Additional query words: sqldmo

Keywords: kbhowto kbinfo KB287737

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.