?
Solved

How to configure a ODBC DSN in your program?

Posted on 1998-12-14
4
Medium Priority
?
473 Views
Last Modified: 2013-11-20
nomally we configure ODBC through control pane,but I
want to configure a ODBC DSN When my program start running.
My database file is "c:\txl.mdb"(using Access 97)
0
Comment
Question by:aiqing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:The_Brain
ID: 1326156
Go to the control panel and press on the ODBC icon.  And do your config there and link it with

ODBC=? DSN=?

? Being paths.

This isn't too much.  but I will know more tommorow.  But do this and see what happens.  (check the help on ODBC link.)
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1326157
Go to the control panel and press on the ODBC icon.  And do your config there and link it with in the code.  when defining ODBC.

ODBC=? "PATH"
 DSN=? "PATH"

? Being paths.

This isn't too much.  but I will know more tommorow.  But do this and see what happens.  (check the help on ODBC link.)
0
 
LVL 1

Accepted Solution

by:
V245 earned 60 total points
ID: 1326158
You should use SQLConfigDataSource.
0
 

Expert Comment

by:WallaceM
ID: 1326159
I just thought I would expand on V245's answer.  Here is a routine I wrote to configure a Microsoft Access Data Source in code:

///////////////////////////////////////////////////////////////
//Routine to ensure that the proper ODBC drivers are installed.
//
//lpszDataSourceFilePath is the path the the
//Microsoft Access database file being installed
//e.g. c:\dmds\bin\dmds_database.mdb
//
//lpszDataSourceName is the name to be assigned
//
BOOL CDBMngr::bConfigODBC
      (const char *lpszDataSourceFilePath,
       const char *lpszDataSourceName)
{
      Trace ("\nCDBMngr::bConfigODBC") ;

      //for routine auditing
      CString csRoutineName = "CDBMngr::bConfigODBC" ;
      CString csLog ;

      BOOL bDirChgd = FALSE ;
      CHAR cCurPath [_MAX_PATH] ;

      CString csTmp ;

      BOOL bresult ;
      CHAR cPath [_MAX_PATH] ;
      WORD wPathlength ;
      bresult = SQLInstallDriverManager
            ( cPath,
              _MAX_PATH,
              &wPathlength ) ;
      Trace
            ("\nCDBMngr::bConfigODBC, called SQLInstallDriverManager w\\result %d",
             bresult ) ;


      //This will start the ODBC driver
      //manager.
      //SQLManageDataSources (m_pMainWnd->m_hWnd) ;

      //NOTE:  See the following help topic for
      //a detailed discussion on how to configure
      //ODBC drivers.
      //
      //"FAQ: Programmatically Configuring an ODBC Data Source"

      //This is how to install the ODBC Foxpro
      //drivers from an installation
      //diskette.  Only do this if you don't
      //have foxpro ODBC drivers installed
      //in the first place.

      //A temporary buffer
      CHAR cTmp [500] ;

      //Variable to hold info specific to
      //the Data Base we are interfacing with.
      //The will be set based on the kind
      //of DB we are talking to.
      CString csDBName ;
      CString csDSN_Name ;
      CString csDataSourceName ;
      CHAR cDSN_Name [100] ;
      CHAR cConfig [500] ;

      //
      //Set up DataBase specific stuff.  Note that many
      //of the values set to cConfig are derived from
      //the Registry at:
      //
      //[HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\
      //
      //What you would do is, install the DSN you
      //need by hand, then go to the registry and
      //see what the settings are.  Edit the
      //code below approriately to match.
      //
      csDBName = "Microsoft Access Driver (*.mdb)" ;
      csDSN_Name = "Microsoft Access Driver (*.mdb)" ;
      csDataSourceName = lpszDataSourceFilePath ;

      csTmp = csDSN_Name ;
      csTmp += "|" ;
      ConvertCharToNull
            ( '|',
              csTmp,
              cDSN_Name,
              sizeof (cDSN_Name)) ;

      //Tack on the datasource name
      csTmp = "DSN=" ;
      //csTmp += WC_ACCESS_DSN_CONST ;
      csTmp += lpszDataSourceName ;
      csTmp += "|" ;

      csTmp += "Description=Access Dissemination Profiles|" ;
      csTmp += "FIL=Access|" ;
      csTmp += "DBQ=" ;
      csTmp += csDataSourceName ;
      csTmp += "|" ;
      ConvertCharToNull
            ( '|',
              csTmp,
              cConfig,
              sizeof (cConfig)) ;

      //See what ODBC drivers are available.  If a
      //driver is not installed, display a message to
      //the user and exit the program.
      WORD wBufSz ;
      bresult = SQLGetInstalledDrivers
            ( cTmp,
              sizeof (cTmp),
              &wBufSz) ;
      Trace
            ("\nCDBMngr::bConfigODBC, called SQLGetInstalledDrivers w\\result %d",
             bresult ) ;


      if (bresult)
      {
            bresult = FALSE ;

            //parse the list returned to see if the proper
            //driver is installed.
            int x = 0 ;
            char *pcTmp ;
            CString csTmp ;
            while (1)
            {
                  if (x>wBufSz) break ;

                  pcTmp = &cTmp[x] ;

                  if (pcTmp != 0)
                     csTmp += pcTmp ;
      
                  if (csTmp.Find (csDBName.GetBuffer(0)) >= 0)
                  {
                        //Found the driver we need, don't
                        //look anymore.
                        bresult = TRUE ;
                        break ;
                  } ;
                  x += (strlen(pcTmp) + 1);

                  if (cTmp[x] == 0) break ;
            } ;
    };

      if (!bresult)
      {
            Trace
                  ("\nCDBMngr::bConfigODBC, need to install %s drivers",
                   csDBName.GetBuffer(0)) ;

            csLog = "The following ODBC database file is not installed.  " ;
            csLog += "Please run the appropriate ODBC installation program and " ;
            csLog += "restart this program.:\n\n" ;
            csLog += csDBName ;
            AfxMessageBox (csLog.GetBuffer(0)) ;

            csLog.Format ("%s not installed", csDBName.GetBuffer(0)) ;
            AfxMessageBox (csLog.GetBuffer(0)) ;

            goto error_return ;
      };

      //////////////////////////////////////////////////////
      //Change back to the current directory before
      //the data source is configured.
    if (bDirChgd)
      {
            //need to change directory back.
            if (_chdir(cCurPath) == -1)
            {
                  csLog = csRoutineName ;
                  csLog += "error restoring current dir." ;
                  AfxMessageBox (csLog.GetBuffer(0)) ;
                  bresult = FALSE ;
            } ;
      };//if changing default directory back
      bDirChgd = FALSE ;
      //////////////////////////////////////////////////////

      //Here is how to remove a data source.  Remove
      //the currently existing one before installing.
      //This will ensure that the ODBC driver is
      //configured properly.
      bresult = ::SQLConfigDataSource
          ( NULL,
          ODBC_REMOVE_DSN,
            csDSN_Name.GetBuffer(0),
            cConfig) ;

      //Now reconfigure the driver just
      //removed.
      bresult = ::SQLConfigDataSource
          ( NULL,
            ODBC_ADD_DSN,
              csDSN_Name.GetBuffer(0),
              cConfig) ;

      goto error_return ;

error_return: ;

    if (bDirChgd)
      {
            //need to change directory back.
            if (_chdir(cCurPath) == -1)
            {
                  csLog = csRoutineName ;
                  csLog += "error restoring current dir." ;
                  AfxMessageBox (csLog.GetBuffer(0)) ;
                  bresult = FALSE ;
            } ;
      };//if changing default directory back

      if (!bresult)
      {
            AfxMessageBox ("error configuring DSN") ;
      } ;

      return bresult ;
}

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question