Solved

How to configure a ODBC DSN in your program?

Posted on 1998-12-14
4
403 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
  • 2
4 Comments
 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
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
Comment Utility
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 30 total points
Comment Utility
You should use SQLConfigDataSource.
0
 

Expert Comment

by:WallaceM
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now