?
Solved

how to programatically check and create odbc?

Posted on 2002-04-17
3
Medium Priority
?
346 Views
Last Modified: 2013-11-20
how can I programatically check if a database connection (odbc) is exist and create it if not, without have to manually go to Control Panel->ODBC and create odbc connection.
0
Comment
Question by:eugeneng
[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
3 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6949895
0
 

Accepted Solution

by:
AndyReed earned 120 total points
ID: 6953737
I packaged it up into this class which may help you. It supports connections to MS Access and Oracle 8. You can derive your own class and configure your connection strings for other drivers. To make a connection you need to instantiate one of the derived classes and call InitDataSource();

Regards,
Andy.

// .h file

#if !defined(AFX_ODBCINSTALLERWRAP_H__374F38D3_5084_11D6_8112_00D0B7157451__INCLUDED_)
#define AFX_ODBCINSTALLERWRAP_H__374F38D3_5084_11D6_8112_00D0B7157451__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CODBCInstaller
{
public:

      CODBCInstaller();
      virtual ~CODBCInstaller();

      BOOL InitDataSource();

protected:
      strswp( char **buffer, char c1, char c2 );
      AppendAttribute( LPCTSTR attribute_name, LPCTSTR value, char **buffer );

      char *m_attribute_string;
      char m_driver_name[256];
};


class COracleODBCInstaller : public CODBCInstaller
{
public:

      COracleODBCInstaller(      CString datasource_name,
                                          CString service_name,
                                          CString user_id );
};

class CAccessODBCInstaller : public CODBCInstaller
{
public:

      CAccessODBCInstaller(      CString datasource_name,
                                          CString filename );
};



#endif // !defined(AFX_ODBCINSTALLERWRAP_H__374F38D3_5084_11D6_8112_00D0B7157451__INCLUDED_)


// .cpp file

#include "stdafx.h"
#include "ODBCInstallerWrap.h"
#include <string.h>
#include <odbcinst.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//***************************************************************************************************
//* FUNCTION:            CODBCInstaller::CODBCInstaller
//*
//* DESCRIPTION:      Constructor
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  none
//*
//* PARAMETERS:            none
//*
//* CHANGE LOG:
//*      
CODBCInstaller::CODBCInstaller()
{
      // Allocate memory for connection string
      m_attribute_string = new char[1024];

      // Clear results buffer
      strcpy( m_attribute_string, "" );

      // Derived class must set driver name
      strcpy( m_driver_name, "" );
      
}

//***************************************************************************************************
//* FUNCTION:            CODBCInstaller::~CODBCInstaller
//*
//* DESCRIPTION:      Destructor
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  none
//*
//* PARAMETERS:            none
//*
//* CHANGE LOG:
//*
CODBCInstaller::~CODBCInstaller()
{
      // Deallocate memory
      delete [] m_attribute_string;
}      

//***************************************************************************************************
//* FUNCTION:            CODBCInstaller::InitDataSource
//*
//* DESCRIPTION:      Use the connection string to connect the ODBC connection to the data source.
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  True - Success
//*                              False - Failure
//*
//* PARAMETERS:            none
//*
//* CHANGE LOG:
//*
BOOL CODBCInstaller::InitDataSource()
{
      BOOL fResult = FALSE;

      // Re-create the ODBC datasource
    fResult = SQLConfigDataSource( NULL,
                                    ODBC_ADD_SYS_DSN, /* ODBC_ADD_DSN OR ODBC_ADD_SYS_DSN */
                                    m_driver_name,
                                    m_attribute_string );

      return fResult;
}

//***************************************************************************************************
//* FUNCTION:            CODBCInstaller::AppendAttribute
//*
//* DESCRIPTION:      Append a single attribute to the attribute string. Initially separate with
//*                              semi-colons but later these must be replaced with NULL characters. Done this
//*                              way because string functions only process up to the first NULL character.
//*
//*                              Appends a string in the format:
//*                              Atribute=Value;
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  void
//*
//* PARAMETERS:            1. Attribute name
//*                              2. Attribute value
//*                              3. Pointer to string buffer to recieve results
//*
//* CHANGE LOG:
//*
CODBCInstaller::AppendAttribute(      LPCTSTR attribute_name,
                                                      LPCTSTR value,
                                                      char **buffer )
{
      strcat( *buffer, attribute_name );
      strcat( *buffer, "=" );
      strcat( *buffer, value );
      strcat( *buffer, ";" );
}

//***************************************************************************************************
//* FUNCTION:            CODBCInstaller::strswp
//*
//* DESCRIPTION:      Swap all occurances of a particular character within a string. All occurances of
//*                              character c1 is replaced with character c2.
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  void
//*
//* PARAMETERS:            1. Pointer to string buffer to recieve results
//*                              2. Search character
//*                              3. Replace character
//*
//* CHANGE LOG:
//*
CODBCInstaller::strswp(char **buffer, char c1, char c2)
{
      size_t i;
      size_t sz = strlen(*buffer);

      for( i=0; i<sz; i++ )
      {
            if( (*buffer)[i] == c1 )
            {
                  (*buffer)[i] = c2;
            }
      }
}

//***************************************************************************************************
//* FUNCTION:            COracleODBCInstaller::COracleODBCInstaller
//*
//* DESCRIPTION:      Constructor, Construct the disconnection/connection strings
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  none
//*
//* PARAMETERS:            none
//*
//* CHANGE LOG:
//*
COracleODBCInstaller::COracleODBCInstaller(      CString datasource_name,
                                                                  CString service_name,
                                                                  CString user_id ) : CODBCInstaller()
{
      // Set driver name
      strcpy( m_driver_name, "Oracle ODBC Driver" );

      // Set driver connection attribute string
      AppendAttribute( "DSN",            datasource_name,      &m_attribute_string );
      AppendAttribute( "Server",      service_name,            &m_attribute_string );
      AppendAttribute( "User",      user_id,                  &m_attribute_string );
      
      // Replace separating semicolons with NULL characters
      strswp( &m_attribute_string, ';', '\0' );
}


//***************************************************************************************************
//* FUNCTION:            CAccessODBCInstaller::CAccessODBCInstaller
//*
//* DESCRIPTION:      Constructor, Construct the disconnection/connection strings
//*
//* CREATED BY:            A Reed
//*
//* DATE:                  April 2002
//*
//* RETURN:                  none
//*
//* PARAMETERS:            none
//*
//* CHANGE LOG:
//*
CAccessODBCInstaller::CAccessODBCInstaller(      CString datasource_name,
                                                                  CString filename ) : CODBCInstaller()
{
      // Set driver name
      strcpy( m_driver_name, "Microsoft Access Driver (*.mdb)" );

      // Set driver connection attribute string
      AppendAttribute( "DSN",            datasource_name,      &m_attribute_string );
      AppendAttribute( "FIL",            "MS Access",            &m_attribute_string );
      AppendAttribute( "DBQ",            filename,                  &m_attribute_string );
      
      // Replace separating semicolons with NULL characters
      strswp( &m_attribute_string, ';', '\0' );
}

0
 

Author Comment

by:eugeneng
ID: 6958644
Excellent!!! thanx alot AndyReed
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 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