Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

How to configure a ODBC DSN in your program?

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
aiqing
Asked:
aiqing
  • 2
1 Solution
 
The_BrainCommented:
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
 
The_BrainCommented:
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
 
V245Commented:
You should use SQLConfigDataSource.
0
 
WallaceMCommented:
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now