Click here to Skip to main content
13,260,330 members (43,669 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 25 Apr 2000

SQL Server and Database Enumerator

, 25 Apr 2000
Rate this:
Please Sign up or sign in to vote.
A class that helps enumerate SQL servers, databases and languages.
  • Download demo executable - 8 Kb
  • Download source files - 33 Kb
  • Sample Image - SQLSrvEnumerator.jpg


    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)), 
    			//if the look up key is foundfill in the result set
    		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.


    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


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


    About the Author

    Santosh Rao
    India India
    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

    You may also be interested in...


    Comments and Discussions

    GeneralThanks Pin
    PJ Arends7-Jan-11 8:18
    memberPJ Arends7-Jan-11 8:18 
    AnswerDrivers for later generations of SQL-server Pin
    Jonas Hammarberg16-May-09 3:48
    memberJonas Hammarberg16-May-09 3:48 
    QuestionUsage of code in commercial offering? Pin
    JHey20084-Nov-08 16:11
    memberJHey20084-Nov-08 16:11 
    GeneralOther databases Pin
    Flora PL10-Sep-04 4:08
    memberFlora PL10-Sep-04 4:08 
    GeneralCannot retrieve list of Databases on Trusted connections Pin
    Anonymous25-Oct-02 1:09
    sussAnonymous25-Oct-02 1:09 
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin) Pin
    Jacques Beaurain4-Feb-03 12:40
    sussJacques Beaurain4-Feb-03 12:40 
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin) Pin
    BSR15-Jan-04 3:44
    memberBSR15-Jan-04 3:44 
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin) Pin
    BSR15-Jan-04 3:45
    memberBSR15-Jan-04 3:45 
    GeneralRe: Cannot retrieve list of Databases on Trusted connections (Fixed, Added RetrieveLogin) Pin
    jbeaurain15-Jan-04 4:15
    memberjbeaurain15-Jan-04 4:15 
    GeneralInternationalization BUG & Repair in SQL Server and Database Enumerator Pin
    gbalog17-Jul-02 3:03
    membergbalog17-Jul-02 3:03 
    GeneralThe modifyed File is here for Internationalization BUG & Repair in SQL Server and Database Enumerator Pin
    gbalog17-Jul-02 3:04
    membergbalog17-Jul-02 3:04 
    GeneralRe: The modifyed File is here for Internationalization BUG & Repair in SQL Server and Database Enumerator Pin
    Santosh Rao17-Jul-02 19:57
    memberSantosh Rao17-Jul-02 19:57 
    GeneralSQLBrowseConnect Pin
    Simone Giannecchini16-Dec-01 2:55
    memberSimone Giannecchini16-Dec-01 2: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 the latest, but none of them run properly. Dead | X| Cry | :((
    Dou you think you can help me?Confused | :confused:
    #define BRWS_LEN 500
    RETCODE rcode;
    HENV henv;
    HDBC hdbc;

    rcode = SQLAllocEnv(&henv);
    if (rcode == SQL_SUCCESS||rcode==SQL_SUCCESS_WITH_INFO)

    SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
    rcode = SQLAllocConnect(henv, & hdbc);
    if (rcode == SQL_SUCCESS||rcode==SQL_SUCCESS_WITH_INFO)

    SQLCHAR szConnStrOut[BRWS_LEN],szConnStrIn[BRWS_LEN];
    SQLSMALLINT cbConnStrOut;
    lstrcpy((TCHAR*)szConnStrIn, "DRIVER={Microsoft Access Driver (*.mdb)};");
    SQLBrowseConnect(hdbc,(SQLCHAR*) szConnStrIn, SQL_NTS,szConnStrOut,BRWS_LEN,&cbConnStrOut);

    I thank all for the help.Blush | :O

    Rose | [Rose] Rose | [Rose]

    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
    Web03 | 2.8.171114.1 | Last Updated 26 Apr 2000
    Article Copyright 2000 by Santosh Rao
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid