Kevin
Kevin

Reputation: 21

Connect to Informix using Microsoft Visual C++

I'm trying to connect to Informix on HP-UX through Microsoft Visual C++ 2010. Any help with ODBC or another way of connecting to Informix will be appreciated.

It's a data form that needs to query, add, update and delete data from various Informix tables.

Upvotes: 0

Views: 1275

Answers (3)

Michał Niklas
Michał Niklas

Reputation: 54332

At first on client machine you must install client. You can download it from IBM site and its name is like: clientsdk.3.70.TC5DE.WIN.zip. Choose newest and install it. Configure client using setnet32 (Menu Start/IBM Informix Client SDK/setnet32). Then configure ODBC source. When asked for driver use "IBM Informix ODBC Driver". There on Connection tab you have to fill some data (host, port, database etc ). You will also see "Apply & Test Connection" button. Use it. If it works you can start programming.

Upvotes: 1

Satyan
Satyan

Reputation: 1406

The .NET driver has the most trivial setup among these drivers.
Assuming you have already done client server connectivity setup on your system by using SetNet32.

Then you may need to add reference to the .net provider binary (IBM.Data.Informix.dll) to your VS project, that is all needed as part of setup.
This binary is located at bin\netf<.net framework version>

For example:C:\CSDK\bin\netf40\

A sample connection string: "Database=MyDb1;UID=myuser;PWD=MyPassword;Server=MyServerInst";


    If you are using C/C++ application then ODBC driver may be a better choice.
    These are the two most common usages of ODBC driver.
    Using ODBC driver directly
    Using ODBC driver through a driver manager.

    Here is the instruction to use Informix ODBC driver directly.
    (If you want to use by using driver manager then change the include and library accordingly from the instruction).

    1) Create VS 2010 C++ Console Application Project
    File -> New Project
    Visual C++
    Win32 Console Application.
    Create an Empty Project 
    <Give Project Name>
    <Press OK>
    <Application Option Check Mark 'Empty Project'>
    <Press Finish Button>

    2) Setup Informix ODBC driver reference to the project
{

    From the Solution Explorer Right Click on the Project Name
    From the pop-up menu that appear, select Property
    From the dialog box do the following setting. 

    Configuration Properties :
    General
    Select appropriate value for 'Character Set'
    If you are not using Unicode API then make sure you have selected 
    Use Multi Byte Character Set


    C/C++:
    General 
    Additional Include Directories
    (Give the include directory location)
    C:\CSDK\incl\cli

    Linker
    General
    Additional Library Directories
    (Give the LIB directory location)
    C:\CSDK\lib

    Linker:
    Command Line
    Additional Options
    (Specify the Informix ODBC driver library)
    iclit09b.lib
}

Your project is ready to use Informix ODBC driver to connect any Informix Dynamic Servers.

//////////////////////////////////////////////// Here is a very simple ODBC code ////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#ifdef WIN32 || WIN64
#ifdef WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif

#ifdef USE_DRIVER_MANAGER
#include "sql.h"
#include "sqlext.h"
#else
#include <infxcli.h>
#endif

void GetDiagRec(SQLRETURN rc, SQLSMALLINT htype, SQLHANDLE hndl, char *szFunTag );

int main( int argc, char *argv[] )
{
    SQLHENV         henv=NULL;
    SQLHDBC         hdbc=NULL;
    SQLRETURN       rc = 0;

    SQLCHAR         ConnStrIn[1024]=
        "DRIVER={IBM INFORMIX ODBC DRIVER};SERVER=MyServerInst;DATABASE=MyDatabase;HOST=HpHost.ibm.com;PROTOCOL=onsoctcp;SERVICE=5550;UID=MyUser;PWD=PasswordForMyUser;";

    rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv );
    rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,0);
    rc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc );

    printf( "\n ConnStr=[%s]\n", ConnStrIn);

    rc = SQLDriverConnect( hdbc, NULL, ConnStrIn, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT );
    if (rc != SQL_SUCCESS)
    {
        GetDiagRec(rc, SQL_HANDLE_DBC, hdbc, "SQLDriverConnect" );
        exit(1);
    }

    // Do Some Work Here
     printf( "\n Connection Success! \n", ConnStrIn);

    rc = SQLDisconnect(hdbc);
    rc = SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
    rc = SQLFreeHandle(SQL_HANDLE_ENV,henv);

    return(0);
}



void GetDiagRec(SQLRETURN rc, SQLSMALLINT htype, SQLHANDLE hndl, char *szFunTag )
{ 
    SQLCHAR message[SQL_MAX_MESSAGE_LENGTH + 1];
    SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
    SQLINTEGER sqlcode;
    SQLSMALLINT length;

    if ( szFunTag == NULL )
    {
        szFunTag = "---";
    }

    printf  ( "\n %s: %d : ", szFunTag, rc);

    if (rc >= 0)
    {
       printf  ( " OK [rc=%d]", rc);
    }
    else
    {  
        int i=1;

        printf  ( " FAILED: %i", rc);
       //Get diagnostic record
       while (SQLGetDiagRec(htype,
                            hndl,
                            i,
                            sqlstate,
                            &sqlcode,
                            message,
                            SQL_MAX_MESSAGE_LENGTH + 1,
                            &length) == SQL_SUCCESS)
       {
         printf  ( "\n SQLSTATE          = %s", sqlstate);

         printf( "\n Native Error Code = %ld", sqlcode);

         printf  ( "\n %s", message);
         i++;
       }
       printf  ( "\n-------------------------\n");
    }
}

Upvotes: 0

Satyan
Satyan

Reputation: 1406

Following Informix drivers are supported, and it can be used from Visual Studio including VS2010. ODBC Driver. .NET Driver. OLE DB driver. ESQL/C

Please let me know what specific information you are looking for.

Upvotes: 0

Related Questions