Solved

how to programatically check and create odbc?

Posted on 2002-04-17
3
339 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
3 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6949895
0
 

Accepted Solution

by:
AndyReed earned 30 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 104
if loop in java 3 114
while loop over for loop 7 80
string initialization in java 11 103
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

10 Experts available now in Live!

Get 1:1 Help Now