Click here to Skip to main content
13,505,249 members
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 15 Aug 2005

How To Exactly Enumerate SQL Server From LAN By Using SQLDMO

, 15 Aug 2005
Rate this:
Please Sign up or sign in to vote.
An article on introducing how to use SQLDMO

Sample screenshotSample screenshot


Many people know that SQLDMO can be used to enumerate SQL Server,but perhaps little people know there is a problem when using SQLDMO . If you don’t use SQLDMO correctly in you program,you maybe find that some SQL Server can not be enumerated.

  1. #import "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033\sqldmo.rll" no_namespace
  2. #import "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033\sqldmo.rll"

Use method 1 can get the right result( right image), use method 2 can get left right result(left image). You may notice that the left image lacks one SQL Server machine


I wrote a VC program to enumerate all SQL Server from LAN two weeks ago, like most people i wrote the program with the method 1. The program seemed to work rightly .but I found that one machine which installed SQL Server on could not be listed. I was strange. Then I wrote it in VB,it could Enumerate all SQL Server,I wrote another program by using ODBC, it could enumerate ervery SQL Server. I was more strange. I decided to look for help from Internet . I downloaded many sample files from to test . The results list as follows

  1. Santosh Rao (
    The program can enumerate correctly but it use ODBC not SQLDMO
  2. Armen Hakobyan (
    The .exe file can enumerate correctly ,but the source code can not by compiled in vc 6,And so on

I analyzed my code very carefully,I found the function ListAvailableSQLServers() can always get right amount every time.but spNameList->Item(i) can not return right result every time ,so I even guess that the data type _bstr_t has a bug. I wasted many days on _bstr_t. I certainly had no gain and I was so disappointed.

By chance I rewote my program with method 2,wonder appeared,the new program could enumerate all SQL Server .I was so happy that I wrote this article at once to let other people to share my pleasue.

Using the code

// if you use method 1,then you shold write as 

    _SQLServerPtr spSQLServer;
    HRESULT hr2 = spSQLServer.CreateInstance(__uuidof(SQLServer));
     if (SUCCEEDED(hr2))
           // Get the Application Ptr
           long lServerCount;
           _ApplicationPtr pApplication = spSQLServer->GetApplication();
           if (pApplication) 
                NameListPtr pServerNameList = pApplication->ListAvailableSQLServers();
                if (pServerNameList)
                    lServerCount = pServerNameList->Count;
                    for (long  i=0;  i <  lServerCount; i++)
                        _bstr_t bstrValue(pServerNameList->Item( i ));
                         CString sName((LPCSTR)bstrValue);
                         if (!sName.IsEmpty())
            pApplication = NULL;
        catch (_com_error e)
        AfxMessageBox("Create Error.");
 catch (_com_error e)

 // if you use method 2,the you should write as 
 SQLDMO::_ApplicationPtr spApplication = NULL;
 SQLDMO::_SQLServerPtr spSQLServer;
 HRESULT hr = spSQLServer.CreateInstance(__uuidof(SQLDMO::SQLServer));
  if (FAILED(hr))
   AfxMessageBox("CreateInstance() error!");
 spApplication =spSQLServer->GetApplication();
 SQLDMO::NameListPtr spNameList = spApplication->ListAvailableSQLServers();
 LONG lCount = spNameList->GetCount();  
 for ( LONG l = 1; l <= lCount; l++ )
   m_ctlComboBox.AddString( (LPCTSTR)spNameList->Item( l ) ); //


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wang Xuemin
China China
Hi,hello to everyone. My name is Wang Xuemin and come from China . I like program and i like VC more . My email is,i hope i can receive your letter .

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Mohan Ramachandra5-Apr-11 21:54
memberMohan Ramachandra5-Apr-11 21:54 
GeneralImages missing Pin
gxdata15-Aug-05 17:38
membergxdata15-Aug-05 17:38 
GeneralRe: Images missing Pin
Wang Xuemin11-Sep-05 16:30
memberWang Xuemin11-Sep-05 16:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 15 Aug 2005
Article Copyright 2005 by Wang Xuemin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid