Solved

How to configure a ODBC DSN in your program?

Posted on 1998-12-14
4
437 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
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 30 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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