Nakul Chaudhary
Nakul Chaudhary

Reputation: 26164

How to create database connectivity through C++

In C++, how can I establish an SQL connection to store data in an SQL database?

Upvotes: 1

Views: 2943

Answers (5)

João Augusto
João Augusto

Reputation: 2305

If you targeting windows you can always use the import ability.

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename( "EOF", "EndOfFile" )

Then you could make a wrapper to deal with the SQL.

Little example, just to open close a connection

// H
class CExtAdoDatabase
{

public:
    CExtAdoDatabase( const char* p_cConnectString="", const char* p_cUsername="", const char* p_cPwd="" );
    virtual ~CExtAdoDatabase();

    bool Open( const char* p_cConnectString="", const char* p_cUsername="", const char* p_cPwd="" );
    bool Close();



private:
    HRESULT _hInitRes;
    bool _bIsValid;

    _ConnectionPtr *_p_pConnection;
};



// CPP
CExtAdoDatabase::CExtAdoDatabase( const char* p_cConnectString, const char* p_cUsername, const char* p_cPwd ) : _hInitRes( CoInitialize( NULL ))
{
    _p_pConnection = new _ConnectionPtr( "ADODB.Connection" );

    if( FAILED( _hInitRes ))
            _bIsValid = false;
    else
    {
        _bIsValid = true;
        (*_p_pConnection)->ConnectionTimeout=0;
        (*_p_pConnection)->CommandTimeout=0;

        if( p_cConnectString != NULL && strlen(p_cConnectString) )
        {
            _bstr_t scs( p_cConnectString );
            _bstr_t susr( p_cUsername );
            _bstr_t spwd( p_cPwd );
            (*_p_pConnection)->Open( scs, susr, spwd, NULL );
        }
    }
}
CExtAdoDatabase::~CExtAdoDatabase()
{
    Close();
    delete _p_pConnection;
    CoUninitialize();
}

bool CExtAdoDatabase::Open( const char* p_cConnectString, const char* p_cUsername, const char* p_cPwd )
{
    if(_bIsValid)
    {
        _bstr_t scs( p_cConnectString );
        _bstr_t susr( p_cUsername );
        _bstr_t spwd( p_cPwd );
        return ((*_p_pConnection)->Open( scs, susr, spwd, NULL ) == S_OK);
    }
    else
        return false;
}

bool CExtAdoDatabase::Close()
{
    if( _bIsValid )
    {
        if( (*_p_pConnection)->GetState() == adStateOpen )
            return !!(*_p_pConnection)->Close();
        else
            return true;
    }
    else
        return false;
}

Upvotes: 0

Rob
Rob

Reputation: 78628

Use SQLAPI++ - it's cross platform and supports MS SQL Server, Oracle, Postgres and others. Very easy to use.

http://www.sqlapi.com/

Upvotes: 0

Tobias Langner
Tobias Langner

Reputation:

you could try wxSqlite with SQLite as Database. This offers you an open source connection header / c++ file to get started.

In general - you should get some kind of library that offers you the required functionality. All major DB vendors should offer at least a C library. Most of the time you get a C++ library or wrapper for the C one.

Upvotes: 1

PW.
PW.

Reputation: 3725

You should have a look at C preprocessors that exists traditionaly with databases (ecpg for postgres, Pro*C for oracle ... which lets you embed straight SQL directly in your source files) or an orginal system for mysql. ECPG will do with C++, that is/was not the case for some other preprocessors ...

Upvotes: 3

HS.
HS.

Reputation: 2615

IF you are targetting Windows, then you might want to use ODBC.

Upvotes: 3

Related Questions