Click here to Skip to main content
Click here to Skip to main content

SQL Server and Database Enumerator

By , 25 Apr 2000
 
  • Download demo executable - 8 Kb
  • Download source files - 33 Kb
  • Sample Image - SQLSrvEnumerator.jpg

    Introduction

    This sample helps you to enumerate the list of SQL Servers and the databases and languages that a particular SQL server has.

    The class CSQLInfoEnumerator encapsulates this functionality into it.

    It has the functions:

    BOOL EnumerateSQLServers();
    BOOL EnumerateDatabase(LPCTSTR pszSQLServer,LPCTSTR pszUserId,LPCTSTR pszPwd);
    BOOL EnumerateDatabaseLanguage(LPCTSTR pszSQLServer,LPCTSTR pszUserId,LPCTSTR pszPwd);

    to perform this task. It fills in its result into the CStringArray m_szSQLServersArray, m_szSQLServerDatabaseArray, and m_szSQLServerLanguageArray data members respectively.

    The heart of this class uses the function SQLBrowseConnect that enables you to build upon an incomplete connect string.

    Example of a connect string:

    ODBC;Driver={SQL Server};SERVER=MYSQLSERVER;APP=MFCAPP;WSID=San;DATABASE=mydb;UseProcForPrepare=0; UID=san;PWD=123

    Note: A connect string is used to establish a database connection using the CDatabase Open or OpenEx member functions.

    Passing an incomplete connect string such as ">Driver={SQL Server};." would cause retrieval of a list of SQL servers. When passed to the SQLBrowseConnect it would retrieve of a list of SQL servers as the server information is missing in the connect string. By passing "Driver={SQL Server};SERVER=MYSQLSERVER; APP=MFCAPP; WSID=San;UID=san;PWD=123;UseProcForPrepare=0;" it would retrieve a list of databases since the database information is missing. The RetrieveInformation function in the CSQLInfoEnumerator class encapuslates this function.

    The function SQLDisconnect has to be called at the end of the SQLBrowseConnect browsing operation completion.

    The complete function RetrieveInformation is as follows.

    //Allocate the environment handle
    m_iRetcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hSQLEnv);
    
    if (m_iRetcode == SQL_SUCCESS || m_iRetcode == SQL_SUCCESS_WITH_INFO)
    {
    	//Set the environment attribute to SQL_OV_ODBC3
    	m_iRetcode = SQLSetEnvAttr(hSQLEnv,SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
    	if (m_iRetcode == SQL_SUCCESS || m_iRetcode == SQL_SUCCESS_WITH_INFO) 
    	{
    		//Allocate a connection handle
    		m_iRetcode = SQLAllocHandle(SQL_HANDLE_DBC, hSQLEnv, &hSQLHdbc);
    		if (m_iRetcode == SQL_SUCCESS || m_iRetcode == SQL_SUCCESS_WITH_INFO) 
    		{
    			CString szConnStrOut;
    			//Call SQLBrowseConnect for additional information
    			m_iRetcode = SQLBrowseConnect(hSQLHdbc, (SQLCHAR *)pszInputParam, SQL_NTS, 
    			                              (SQLCHAR *)(szConnStrOut.GetBuffer(MAX_RET_LENGTH)), 
    			                              MAX_RET_LENGTH,&sConnStrOut);
    			szConnStrOut.ReleaseBuffer();
    			
    			//if the look up key is foundfill in the result set
    			SQLDisconnect(hSQLHdbc);
    		}
    		SQLFreeHandle(SQL_HANDLE_DBC, hSQLHdbc);
    	}
    	SQLFreeHandle(SQL_HANDLE_ENV, hSQLEnv);
    }
    

    The CSQLInfoEnumerator class requires linking with the odbc32.lib file.

    The sample application attached uses the CSQLInfoEnumerator class to display the list of SQL Servers, databases and supported languages.

    Limitations

    When the list of languages supported by a particular SQL server is performed, it does not support character translation for the language name meaning you may see special characters and ?'s in the language listing for particular SQL Servers.

    You can reach me at SantoshRao@bigfoot.com

    License

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

    About the Author

    Santosh Rao
    Architect
    India India
    Member
    1993 started with Computers
     
    BE(Computer Science) and MS (Software Systems)
     
    Industry Experience: 10 Years
     
    C, C++, VC++(MFC), .NET, C#, MTS, Queuing, ASP.NET, AJAX, Java, J2EE, SunOne, JMS
     
    Banking, Insurance & Pension,Health Care

    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

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralThanksmemberPJ Arends7 Jan '11 - 7:18 
    This is just what I have been looking for. Up until now all I have been able to find is the newer .net stuff that only does half of what this class does.   5 from me You may be right I may be crazy -- Billy Joel --   Within you lies the power for good - Use it!
    AnswerDrivers for later generations of SQL-servermemberJonas Hammarberg16 May '09 - 2:48 
    Hi   As I've been using the same approach for getting all servers for SQL-Server and only getting SQL-2000, 7 and earlier. Missing 2005 and 2008. By supplying later drivers all supporting servers will be returned.   SQL2008: Driver={SQL Server Native Client 10.0} SQL2005:...
    QuestionUsage of code in commercial offering?memberJHey20084 Nov '08 - 15:11 
    Hi,   I would like to use parts of your code in a commercial offering. Can I have your permission to do so? (I tried your email, but doesn't work anymore)
    GeneralOther databasesmemberFlora PL10 Sep '04 - 3:08 
    Is it posible to adopt this code to list other servers eg. Informix or Oracle ?
    GeneralCannot retrieve list of Databases on Trusted connectionssussAnonymous25 Oct '02 - 0:09 
    I cannot seem to retrieve a list of Databases on Trusted connections. My server is SQL 2000 and the user name and pass are handled via Windows Authentication.   Any ideas on the connection string which should be used to retrieve database names.   Thanks,   Andy
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin)sussJacques Beaurain4 Feb '03 - 11:40 
    I changed the class to handle trusted connections. I also added code to retrieve/test login information and fixed the member to return false on errors. The commented functions are an external error handler. Here is the code (This includes the language fix).   Hope someone finds this useful....
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin)memberBSR15 Jan '04 - 2:44 
    Where are these 3 files available?   #include #include #include   Regards BSR
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin)memberBSR15 Jan '04 - 2:45 
    Where are these 3 files available? sys.h, geosoft.h and ap.h   regards BSR
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin)memberjbeaurain15 Jan '04 - 3:15 
    Sorry about that, they are internal GXs we use to use internal functions here and include them in most of our C/C++ sources. You can safely delete the 3 lines.
    GeneralInternationalization BUG & Repair in SQL Server and Database Enumeratormembergbalog17 Jul '02 - 2:03 
    Hi Santosh,   thank a lot for your super Classes. I got a problem with non English SQL Servers, because p.e. the following line return RetrieveInformation(szInputParam,_T("DATABASE:Database="),m_szSQLServerDatabaseArray); in German should be: return...
    GeneralThe modifyed File is here for Internationalization BUG & Repair in SQL Server and Database Enumeratormembergbalog17 Jul '02 - 2:04 
    // SQLInfoEnumerator.cpp: implementation of the CSQLInfoEnumerator class. // //////////////////////////////////////////////////////////////////////   #include "stdafx.h" #include "SQLInfoEnumerator.h"   #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new...
    GeneralRe: The modifyed File is here for Internationalization BUG & Repair in SQL Server and Database EnumeratormemberSantosh Rao17 Jul '02 - 18:57 
    Hi Buddy,   That is a nice addition.   Thanks & Best Wishes Santosh  
    GeneralSQLBrowseConnectmemberSimone Giannecchini16 Dec '01 - 1:55 
    First of all i apologize for my bad english. I'm in trouble with the piece of code that is below. I'd like to discover programmatically the exact format of the connection string for the Microsoft Access Driver using SQLBrowseConnect. I tried several different formats for this function, here is...

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.6.130516.1 | Last Updated 26 Apr 2000
    Article Copyright 2000 by Santosh Rao
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid