Click here to Skip to main content
Licence CPOL
First Posted 10 Mar 2002
Views 110,057
Downloads 3,795
Bookmarked 13 times

List SQL Servers on a Network

By | 10 Mar 2002 | Article
An activeX DLL which lists any active SQL servers on your local network

Introduction

This is just something that I needed for a recent application and thought that others may find it useful.

The problem was to enumerate all available SQL Servers on a local network. I decided that the easiest way to do this was to create a VB activeX DLL which could then be referenced by whatever needed to use it (I've tested it in .NET, VB and ASP).

I'm not going to provide a code walkthrough with this, but I've included the VB project and compiled DLL in the source code zip, plus there's a very simple VB application which shows its use.

Essentially it works by making an API call to the netApi32 library (the function it uses is NetServerEnum).

When I have some more time, I will update this article with a full explanation of the code. For now however, things should work pretty much "out of the box", and will hopefully be useful to some of you.

Any problems, then just let me know.

History

  • 10th March, 2002: Initial post

License

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

About the Author

Torsten Mauz

Web Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow i can do it at c#? Pinmemberblud0ff4:04 18 Oct '05  
GeneralAnother (maybe simpeler?) approach PinmemberRogier Reedijk1:29 12 Mar '02  
I once needed the exact same functionality and found the following code somewhere on the internet:
 
#include < sql.h >
#include < sqlext.h >
 
BOOL EnumerateServers()
{
   LPCTSTR pszInputParam = _T("Driver={SQL Server}");
   LPCTSTR pszLookUpKey = _T("SERVER:Server=");
 
   SQLHENV  hSQLEnv;
   SQLHDBC  hSQLHdbc;
   short    sConnStrOut;
   DWORD    dwRetCode;
 
   //Allocate the environment handle
   dwRetCode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hSQLEnv);
 
   if (dwRetCode == SQL_SUCCESS || dwRetCode == SQL_SUCCESS_WITH_INFO)
   {
      //Set the environment attribute to SQL_OV_ODBC3
      dwRetCode = SQLSetEnvAttr(hSQLEnv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
 
      if (dwRetCode == SQL_SUCCESS || dwRetCode == SQL_SUCCESS_WITH_INFO) 
      {
         //Allocate a connection handle
         dwRetCode = SQLAllocHandle(SQL_HANDLE_DBC, hSQLEnv, &hSQLHdbc);
 
         if (dwRetCode == SQL_SUCCESS || dwRetCode == SQL_SUCCESS_WITH_INFO) 
         {
            CString szConnStrOut;
 
            //Call SQLBrowseConnect for additional information
            dwRetCode = SQLBrowseConnect(hSQLHdbc, 
                                         (SQLCHAR *)pszInputParam, 
                                         SQL_NTS,
                                         (SQLCHAR *)(szConnStrOut.GetBuffer(ENUM_SERVERS_MAX_RET_LENGTH)),
                                         ENUM_SERVERS_MAX_RET_LENGTH,
                                         &sConnStrOut);
            szConnStrOut.ReleaseBuffer();
 
            //if the look up key is found
            //fill in the result set
            int iFind = szConnStrOut.Find(pszLookUpKey);
            if(iFind != -1)
            {
               CString szLookUpKey = pszLookUpKey;
               szConnStrOut = szConnStrOut.Mid(iFind+szLookUpKey.GetLength());
 
               iFind = szConnStrOut.Find('{');
               if(iFind != -1)
               {
                  szConnStrOut = szConnStrOut.Mid(iFind+1);
                  iFind = szConnStrOut.Find('}');
                  if(iFind != -1)
                  {
                     szConnStrOut = szConnStrOut.Left(iFind);
 
                     // szConnStrOut contains all servers seperated by a comma 
                     // do with it what you like

                     dwRetCode = TRUE;
                  }
               }
            }	
 
            SQLDisconnect(hSQLHdbc);
         }
         SQLFreeHandle(SQL_HANDLE_DBC, hSQLHdbc);
      }
      SQLFreeHandle(SQL_HANDLE_ENV, hSQLEnv);
   }
   return dwRetCode;
}
I can't remember where I found it, but it works fine for me. Smile | :)
 
Calling the NetServerEnum function seems cleaner but the only problem is it won't function on Windows 9x/ME (according to the MSDN). My function will. Smile | :)
 
Rogier.
GeneralRe: Another (maybe simpeler?) approach Pinmembermostevil2:12 21 May '02  
GeneralRe: Another (maybe simpeler?) approach PinmemberRogier Reedijk1:36 22 May '02  
GeneralRe: Another (maybe simpeler?) approach PinmemberPettys11:00 10 Jul '02  
GeneralRe: Another (maybe simpeler?) approach PinsussAnonymous21:46 27 May '03  
GeneralRe: Another (maybe simpeler?) approach PinmemberTorsten Mauz0:20 29 May '03  
GeneralRe: Another (maybe simpeler?) approach Pinmemberhaberdasher6:02 16 Jul '03  
GeneralRe: Another (maybe simpeler?) approach PinmemberPhil Jeary0:06 21 Jun '04  
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pinmemberzjerry5:57 7 Oct '04  
GeneralRe: Thanks! PinmemberLantric2:58 21 Nov '04  
GeneralRe: Another (maybe simpeler?) approach - in VB.Net PinsussAnonymous23:12 7 Apr '05  
GeneralRe: Another (maybe simpeler?) approach - in VB.Net PinmemberFrank Esser10:26 25 Aug '05  
QuestionRe: Another (maybe simpeler?) approach - in VB.Net Pinmemberrugbygeek10:00 15 Feb '06  
AnswerRe: Another (maybe simpeler?) approach - in VB.Net PinmemberPolymorpher3:44 13 Jun '06  
GeneralRe: Another (maybe simpeler?) approach - in VB.Net PinmemberLokanatha Reddy19:56 16 Jan '07  
GeneralRe: Another (maybe simpeler?) approach - in VB.Net PinmemberPolymorpher3:41 13 Jun '06  
GeneralRe: Another (maybe simpeler?) approach - in VB.Net PinmemberLokanatha Reddy0:53 22 Jan '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120529.1 | Last Updated 11 Mar 2002
Article Copyright 2002 by Torsten Mauz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid