Microsoft KB Archive/119064

= How To Retrieving a List of All ODBC Data Sources =

Article ID: 119064

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li> Microsoft Data Access Components 1.5</li> Microsoft Data Access Components 2.0</li> <li>Microsoft Data Access Components 2.1</li> <li>Microsoft Data Access Components 2.5</li> <li>Microsoft Data Access Components 2.6</li> <li>Microsoft Data Access Components 2.7</li></ul>

-

<div class="notice_section">

This article was previously published under Q119064

<div class="summary_section">

SUMMARY
The Open Database Connectivity (ODBC) API contains a function called SQLDataSources which can be used to retrieve information about data sources which are available to an application. Below is a sample function which fills a CStringList with the names of all available ODBC data sources.

Sample Code
#include <afxcoll.h>   //Needed for CStringList MFC class. #include "odbcinst.h"  #include "sql.h"   #include "sqlext.h"

// NOTE: in 16-bit Visual C++ link with odbcinst.lib //      in 32-bit Visual C++ 2.x link with odbccp32.lib //      in 32-bit Visual C++ 4.x no need to change link options

#define MAX_DSN_LENGTH 30 #define MAX_DSN_DESC_LENGTH 300

BOOL GetODBCDataSourceNames(CStringList * pList) {      HENV hEnv; char szDSN[MAX_DSN_LENGTH]; SWORD cbDSN; UCHAR szDescription[MAX_DSN_DESC_LENGTH]; SWORD cbDescription; RETCODE retcode;

ASSERT(pList->IsEmpty); if (SQLAllocEnv(&hEnv)!=SQL_SUCCESS) return FALSE;

while (retcode=SQLDataSources(hEnv, SQL_FETCH_NEXT, (UCHAR FAR *) &szDSN, MAX_DSN_LENGTH, &cbDSN, (UCHAR FAR *) &szDescription,MAX_DSN_DESC_LENGTH, &cbDescription) != SQL_NO_DATA_FOUND                   &&retcode!=SQL_ERROR)

{              pList->AddTail(szDSN); }

SQLFreeEnv(hEnv); if (retcode==SQL_ERROR) return FALSE;

return TRUE; }

Keywords: kbcode kbdatabase kbhowto kbprogramming KB119064

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.