Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to programatically check and create odbc?

Posted on 2002-04-17
3
Medium Priority
?
349 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

618 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