Browsing for ODBC Data Source Names (DSN)

Environment: VC6 SP4, NT4 SP5

Introduction

This article is the result of some work I did in a recent project. I was improving a configuration program user interface for a complex client-server application where several databases where used. A common configuration option was what DSN (Data Source Name) to use. Since users had found it inconvenient to look the DSN name up using the ODBC configuration tool in the control panel and then type the name manually, we had decided to add a browse for DSN button.

Since I didn't find any standard "browse for DSN dialog" I wrapped the interesting calls to the ODBC API in a simple class called CDSNCollection which then was used to enumerate the DSNs and show them in a home made browse dialog.

The pitfalls

Doing simple things with ODBC API in C++ can easily become complicated. First of all an environment handle has to be created (see CDSNCollection::CDSNCollection for details). Then one must specify which ODBC version to use (see CDSNCollection::CDSNCollection for details). This might seam strange since the latest version probably is used 99% of the time. My guess is that Microsoft has chosen this approach in order to achieve forward capability even when making major changes in the ODBC API. And when you think of it for a while it actually makes more sense than the "classical" Microsoft way to achieve backward and forward capability.

A few thoughts

I don't usually use C++ for heavy database action and this operation is quite simple since it does not use any specific database. Still two calls are needed just to initialize before the simple task of getting a few DSN names. Error handling is also a little special; see the call to SQLGetDiagRec in CDSNCollection::CallSQLDataSources.

Since I just created a simple class which just wraps a simple ODBC API call (initialization and error handling does not really count... :-) there is not much more to say. Just use it if you want to enumerate through your DSNs...

Downloads

Download demo project - 17 Kb
Download source - 3 Kb


Comments

  • Would be helpful to enumerate associated drivers

    Posted by Mike Pliam on 02/25/2007 08:38pm

    This is really a great little program. Using SQLDriverConnect(sqlhdbc HDBC, SQLHWND hwnd, SQLCHAR *szConnStrIn, SQLSMALLINT cbConnStrIn, SQLCHAR * szConnStrOut, SQLSMALLINT cbConnStrOutMax, SQLSMALLINT *pcbConnStrOut, SQLUSMALLINT fDriverCompletion); may work, but I have not figured out how to set the parameters. In any case, it would be nice to have the various drivers listed so that 'not too bright' users like myself would more quickly be able to tell which database there looking for by virtue of the associated driver. Using SQLDriverConnect(...) might assist in providing that information. Is there an example somewhere? There are a number of ways to get the data source dialog to come up, but for many of us, that dialog is simply too complicated.

    Reply
  • How to connect ADO without manually configuring the ODBC

    Posted by Legacy on 01/22/2003 12:00am

    Originally posted by: peevee

    I'm a beginner in using ADO 1.5, Is there a way to use ADO without depending on the DSN that is manually configured in the ODBC. What I mean is, can I Add a DSN in the ODBC at runtime?

    Reply
  • not needed

    Posted by Legacy on 09/22/2002 12:00am

    Originally posted by: Richard Matteson

    to browse the odbc database connections just call the odbc api function : SQLDriverConnect, with the flag SQL_DRIVER_PROMPT and the system ODBC dialog will magically
    appear.

    Reply
  • How to connect Visual C++ with an DB2 Server without ODBC and DAO?

    Posted by Legacy on 08/29/2002 12:00am

    Originally posted by: davy

    Please ~~~
    thank you....

    Reply
  • how to enumerate tables and fields out of it

    Posted by Legacy on 07/30/2002 12:00am

    Originally posted by: Usman

    HI
    I want to use it like when user selcts any of the dsn, the application would populate another list or combo box wiht all the table in the associated database. Next on selecting a particular table, again all the fields of that particular table are populated in another box.
    I'm new with this. so please help me with the code if possible.

    TIA
    Usman

    Reply
  • COM-object connection to Database

    Posted by Legacy on 04/29/2002 12:00am

    Originally posted by: cipelic

    Hello!
    I created my COM-object with ATL App Wizard and I choose MEF support because I want COM-object to be able to use CRecordset class.

    I created ODBC data source and than I add a new class to my COM-object. This class derives from CRecordset so that I can make connection to my SQL-Database.

    This COM-object is going to be used by ASP but when I open the first asp-page which creates this COM-object and use it's interface method(connecting SQL) I get this error that says something is wrong in COM-object, part that connect SQL.

    This is how I wrote one interface method...
    Do you know what can be wrong..
    Can you recomend some good tutorial about ATL COM and ODBC

    STDMETHODIMP CCOM::GetPers()
    {
    CDatabase m_DB;
    CPerson Pers;
    int funnen = 0;

    CAnvSet* m_pers = new CAnvSet(&m_DB);

    CString tab_namn = m_pers->GetDefaultConnect();
    m_DB.Open(NULL, FALSE, FALSE, tab_namn, FALSE);

    m_pers->Open();

    if(!m_pers->IsBOF())
    m_pers->MoveFirst();

    while(!m_pers->IsEOF() && !funnen)
    {
    if(m_pers->m_Namn == namn)
    {
    Pers.SetNamn(m_pers->m_Namn);
    Pers.SetNotation(m_pers->m_Notation);
    Pers.SetTalbas(m_pers->m_Talbas);
    funnen = 1;
    }
    else
    m_pers->MoveNext();
    }
    if(m_pers->IsEOF())
    {
    Pers.SetNamn(namn);
    Pers.SetNotation(0);
    Pers.SetTalbas(3);
    }
    m_pers->Close();
    m_DB.Close();
    delete(m_pers);
    return S_OK;
    }

    Thank you!

    Reply
  • Using SQLConfigDataSource with IBM DB2 ODBC DRIVER

    Posted by Legacy on 10/18/2001 12:00am

    Originally posted by: Aniruddha

    This project is good.But I am facing a lot of problem while
    using SQLConfigDataSource with IBM DB2 ODBC DRIVER.
    The call is successful only if I manually connect to the
    DB2 server/database first.I can't find the right connection
    keyword to use for connecting to the DB2 database server
    at run time.I want the user to be able to enter the details
    such as ServerName,DatabaseName,Protocol etc.
    Please suggest any working ideas.
    Sample code would be very welcome.
    Thanx.
    Aniruddha

    Reply
  • How to connect Visual C++ with an SQL Server??

    Posted by Legacy on 10/07/2001 12:00am

    Originally posted by: Vincentius Jie

    I wanna read & write to SQL Server 7.0 using Visual C++ 6.0. Can I do it??? Is it possible??? How to do it??? What is the function???

    If anyone knows about it please help me through this forum or can send e-mail to ciko@sby.centrin.net.id. I need it to fulfill my final project in my univ. Thanks a lot.

    Reply
  • how to use your wonderful application with ASP

    Posted by Legacy on 06/26/2001 12:00am

    Originally posted by: taylor

    Hello! I am currently developing some web applications using ASP and it has something to do with database too. I wonder if there is a way to achieve the samething(accessing the registered data source names) with ASP?
    Thank you very much!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds