Solved

how to programatically check and create odbc?

Posted on 2002-04-17
3
344 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 115
WinWaitActive parameters 12 31
canBalance challenge 34 108
JQuery serialize and unserialize 8 259
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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