Click here to Skip to main content
15,923,789 members
Articles / Programming Languages / VBScript

List SQL Servers on a Network

Rate me:
Please Sign up or sign in to vote.
2.71/5 (14 votes)
10 Mar 2002CPOL 153.4K   5.3K   13   18
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)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow i can do it at c#? Pin
blud0ff18-Oct-05 4:04
blud0ff18-Oct-05 4:04 
GeneralAnother (maybe simpeler?) approach Pin
Rogier Reedijk12-Mar-02 1:29
Rogier Reedijk12-Mar-02 1:29 
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 Pin
21-May-02 2:12
suss21-May-02 2:12 
GeneralRe: Another (maybe simpeler?) approach Pin
Rogier Reedijk22-May-02 1:36
Rogier Reedijk22-May-02 1:36 
GeneralRe: Another (maybe simpeler?) approach Pin
Pettys10-Jul-02 11:00
Pettys10-Jul-02 11:00 
GeneralRe: Another (maybe simpeler?) approach Pin
Anonymous27-May-03 21:46
Anonymous27-May-03 21:46 
GeneralRe: Another (maybe simpeler?) approach Pin
Torsten Mauz29-May-03 0:20
Torsten Mauz29-May-03 0:20 
GeneralRe: Another (maybe simpeler?) approach Pin
grriffin16-Jul-03 6:02
grriffin16-Jul-03 6:02 
GeneralRe: Another (maybe simpeler?) approach Pin
Phil Jeary21-Jun-04 0:06
Phil Jeary21-Jun-04 0:06 
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pin
Jarosław Zwierz7-Oct-04 5:57
Jarosław Zwierz7-Oct-04 5:57 
GeneralRe: Thanks! Pin
Lantric21-Nov-04 2:58
Lantric21-Nov-04 2:58 
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pin
Anonymous7-Apr-05 23:12
Anonymous7-Apr-05 23:12 
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pin
Frank Esser25-Aug-05 10:26
Frank Esser25-Aug-05 10:26 
QuestionRe: Another (maybe simpeler?) approach - in VB.Net Pin
rugbygeek15-Feb-06 10:00
rugbygeek15-Feb-06 10:00 
AnswerRe: Another (maybe simpeler?) approach - in VB.Net Pin
Polymorpher13-Jun-06 3:44
Polymorpher13-Jun-06 3:44 
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pin
Lokanatha Reddy16-Jan-07 19:56
Lokanatha Reddy16-Jan-07 19:56 
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pin
Polymorpher13-Jun-06 3:41
Polymorpher13-Jun-06 3:41 
GeneralRe: Another (maybe simpeler?) approach - in VB.Net Pin
Lokanatha Reddy22-Jan-07 0:53
Lokanatha Reddy22-Jan-07 0:53 

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.