使用Microsoft Visual C ++连接到Informix

时间:2014-01-29 17:08:51

标签: visual-c++ odbc informix

我正在尝试通过Microsoft Visual C ++ 2010连接到HP-UX上的Informix。我们将非常感谢您使用ODBC或其他方式连接到Informix。

这是一个需要查询,添加,更新和删除各种Informix表中数据的数据表单。

3 个答案:

答案 0 :(得分:1)

首先在客户端计算机上,您必须安装客户端。您可以从IBM站点下载它,其名称如下:clientsdk.3.70.TC5DE.WIN.zip。选择最新并安装它。使用setnet32配置客户端(Menu Start / IBM Informix Client SDK / setnet32)。然后配置ODBC源。当被要求驱动程序使用“IBM Informix ODBC Driver”时。在连接选项卡上,您必须填写一些数据(主机,端口,数据库等)。您还会看到“应用和测试连接”按钮。用它。如果它有效,你可以开始编程。

答案 1 :(得分:0)

支持Informix驱动程序,可以在Visual Studio中使用,包括VS2010。 ODBC驱动程序。 .NET驱动程序。 OLE DB驱动程序。 ESQL / C

请告诉我您要查找的具体信息。

答案 2 :(得分:0)

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.

/////////////////////////////////////////////// /     这是一个非常简单的ODBC代码 ////////////////////////////////////////////////

#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");
    }
}