Step by Step TimesTen-- 使用ODBC訪問TimesTen資料庫C++篇(一個例子)(1)

feng_xin發表於2008-08-29

1,在client端配置ODBC

參見文件:Step by Step TimesTen ----- 配置client-server連線

http://space.itpub.net/81/viewspace-421428

 

2,設定環境變數

ODBCINI=/ora/TimesTen/odbc.ini

SYSTTCONNECTINI=/ora/TimesTen/ttconnect.ini

 

3,程式:

test.cc

 

#ifdef WIN32

#include

#else

#include

#endif

#include

#include

#include

#include

#include

 

void CheckReturnCode(SQLRETURN rc, SQLHENV henv,

                    SQLHDBC hdbc, SQLHSTMT hstmt,

                    char* msg, char *filename,

                    int lineno);

                   

 

int main(int argc, char** argv)

{

    SQLRETURN rc = SQL_SUCCESS;

    /* General return code for the API */

    SQLHENV hEnv = SQL_NULL_HENV;

    /* Environment handle */

    SQLHDBC hDbc = SQL_NULL_HDBC;

    /* Connection handle */

    SQLHSTMT hStmt = SQL_NULL_HSTMT;

    /* Statement handle */

    SQLCHAR ConnOut[255];

    /* Buffer for completed connection string */

    SQLSMALLINT connOutLen;

    /* number of bytes returned in ConnOut */

    SQLCHAR *ConnString = (SQLCHAR *)

        "DSN=test_cs";

   

    /* Connection attributes */

    rc = SQLAllocEnv(&hEnv);

    if (rc != SQL_SUCCESS) {

        fprintf(stderr,"Unable to allocate an "

            "environment handle\n");

        exit(1);

    }

    rc = SQLAllocConnect(hEnv, &hDbc);

    CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,

                    SQL_NULL_HSTMT,

                    "Unable to allocate a "

                    "connection handle\n",

                    __FILE__, __LINE__);

#if 1

    rc = SQLDriverConnect(hDbc, NULL,

        ConnString, SQL_NTS,

        ConnOut, 255,

        &connOutLen,

        SQL_DRIVER_NOPROMPT);

#else

    rc = SQLConnect(hDbc, ConnString,

        SQL_NTS,(SQLCHAR*)"", SQL_NTS,

        (SQLCHAR*)"", SQL_NTS);

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

#endif

    CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT,

        "Error in connecting to the"

        " driver\n",

        __FILE__, __LINE__);

       

 

    rc = SQLAllocStmt(hDbc, &hStmt);

    CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT,

        "Unable to allocate a "

        "statement handle\n",

        __FILE__,__LINE__);

    /* Your application code here */

    if (hStmt != SQL_NULL_HSTMT)

    {

        rc = SQLFreeStmt(hStmt, SQL_DROP);

        CheckReturnCode(rc, hEnv, hDbc, hStmt,

            "Unable to free the "

            "statement handle\n",

            __FILE__, __LINE__);

    }

   

    if (hDbc != SQL_NULL_HDBC)

    {

        rc = SQLDisconnect(hDbc);

        CheckReturnCode(rc, hEnv, hDbc,

            SQL_NULL_HSTMT,

            "Unable to close the "

            "connection\n",

            __FILE__, __LINE__);

        rc = SQLFreeConnect(hDbc);

        CheckReturnCode(rc, hEnv, hDbc,

            SQL_NULL_HSTMT,

            "Unable to free the "

            "connection handle\n",

            __FILE__, __LINE__);

    }

   

    if (hEnv != SQL_NULL_HENV)

    {

        rc = SQLFreeEnv(hEnv);

        CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,

            SQL_NULL_HSTMT,

            "Unable to free the "

            "environment handle\n",

            __FILE__, __LINE__);

    }

}

void CheckReturnCode(SQLRETURN rc, SQLHENV henv,

            SQLHDBC hdbc, SQLHSTMT hstmt,

            char* msg, char *filename,

            int lineno)

{

    #define MSG_LNG 512

    SQLCHAR szSqlState[MSG_LNG];

    /* SQL state string */

    SQLINTEGER pfNativeError;

    /* Native error code */

    SQLCHAR szErrorMsg[MSG_LNG];

    /* Error msg text buffer pointer */

    SQLSMALLINT pcbErrorMsg;

    /* Error msg text Available bytes */

    SQLRETURN ret = SQL_SUCCESS;

   

   

    if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND )

    {

        if (rc != SQL_SUCCESS_WITH_INFO)

        {

            /*

            * It's not just a warning

            */

            fprintf(stderr, "*** ERROR in %s, line %d:"

                " %s\n",

                filename, lineno, msg);

        }

        /*

        * Now see why the error/warning occurred

        */

        while (ret == SQL_SUCCESS ||

        ret == SQL_SUCCESS_WITH_INFO)

        {

            ret = SQLError(henv, hdbc, hstmt,

                szSqlState, &pfNativeError,

                szErrorMsg, MSG_LNG,

                &pcbErrorMsg);

 

blog長度限制,無法貼完全部程式碼,接下篇Step by Step TimesTen-- 使用ODBC訪問TimesTen資料庫C++篇(一個例子)(2)http://space.itpub.net/81/viewspace-438948

 

 

          

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/81/viewspace-438946/,如需轉載,請註明出處,否則將追究法律責任。

相關文章