请到http://www-1.ibm.com/servers/eserver/iseries/db2上看看书

解决方案 »

  1.   

    给你个例子:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sqlda.h>
    #include <sqlcli1.h>
    #include "samputil.h"          /* Header file for CLI sample code */
     
    int SQL_API_FN inpsrv2( struct sqlchar * input_data,
                            struct sqlda   * input_sqlda,
                            struct sqlda   * output_sqlda,
                            struct sqlca   * ca
                          ) {
     
       /* Declare a local SQLCA */
       struct sqlca sqlca ;
     
       SQLCHAR table_stmt[80] = "CREATE TABLE " ;
       SQLCHAR insert_stmt[80] = "INSERT INTO " ;
       SQLCHAR insert_data[21] ;
       SQLINTEGER insert_data_ind ;
     
       /* Delare Miscellanous Variables */
       int cntr ;
       char * table_name ;
       short table_name_length ;
       char * data_item[3] ;
       short data_item_length[3] ;
       int num_of_data = 0 ;
     
       /* Delare CLI Variables */
       SQLHANDLE henv, hdbc, hstmt ;
       SQLRETURN rc ;
     
       /*-----------------------------------------------------------------*/
       /* Assign the data from the SQLDA to local variables so that we    */
       /* don't have to refer to the SQLDA structure further.  This will  */
       /* provide better portability to other platforms such as DB2 MVS   */
       /* where they receive the parameter list differently.              */
       /* Note: Strings are not null-terminated in the SQLDA.             */
       /*-----------------------------------------------------------------*/
     
       table_name = input_sqlda->sqlvar[0].sqldata ;
       table_name_length = input_sqlda->sqlvar[0].sqllen ;
       num_of_data = input_sqlda->sqld - 1 ;
       for ( cntr = 0; cntr < num_of_data; cntr++ ) {
           data_item[cntr] = input_sqlda->sqlvar[cntr+1].sqldata ;
           data_item_length[cntr] = input_sqlda->sqlvar[cntr+1].sqllen ;
       }
     
       /*-----------------------------------------------------------------*/
       /* Setup CLI required environment                                  */
       /*-----------------------------------------------------------------*/
     
       SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
       SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc ) ;
     
       /*-----------------------------------------------------------------*/
       /* Issue NULL Connect, since in CLI we need a statement handle     */
       /* and thus a connection handle and environment handle.            */
       /* A connection is not established, rather the current             */
       /* connection from the calling application is used                 */
       /*-----------------------------------------------------------------*/
       SQLConnect( hdbc, NULL, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS ) ;
       SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ;
     
       /*-----------------------------------------------------------------*/
       /* Create President Table                                          */
       /* - For simplicity, we'll ignore any errors from the              */
       /*   CREATE TABLE so that you can run this program even when the   */
       /*   table already exists due to a previous run.                   */
       /*-----------------------------------------------------------------*/
     
       strncat( ( char * ) table_stmt,
                ( char * ) table_name,
                table_name_length
              ) ;
       strcat( ( char * ) table_stmt, " (name CHAR(20))" ) ;
     
       SQLExecDirect( hstmt, table_stmt, SQL_NTS ) ;
     
       SQLFreeStmt( hstmt, SQL_RESET_PARAMS ) ;
     
       /*-----------------------------------------------------------------*/
       /* Generate and execute a PREPARE for an INSERT statement, and     */
       /* then insert the three presidents.                               */
       /*-----------------------------------------------------------------*/
     
       strncat( ( char * ) insert_stmt,
                ( char * ) table_name,
                table_name_length
              ) ;
       strcat( ( char * ) insert_stmt, "  VALUES (?)" ) ;
     
       if ( SQLPrepare(hstmt, insert_stmt, SQL_NTS) != SQL_SUCCESS ) goto ext ;
     
       /* Bind insert_data to parameter er */
       SQLBindParameter( hstmt,
                         1,
                         SQL_PARAM_INPUT,
                         SQL_C_CHAR,
                         SQL_CHAR,
                         20,
                         0,
                         insert_data,
                         21,
                         &insert_data_ind
                       ) ;
     
       for ( cntr = 0; cntr < num_of_data; cntr++ ) {
           strncpy( ( char * ) insert_data,
                    ( char * ) data_item[cntr],
                    data_item_length[cntr]) ;
           insert_data_ind = data_item_length[cntr] ;
           if ( SQLExecute( hstmt ) != SQL_SUCCESS ) goto ext ;
       }
     
       /*-----------------------------------------------------------------*/
       /* Return to caller                                                */
       /*   -  Copy the SQLCA                                             */
       /*   -  Update the output SQLDA.  Since there's no output to       */
       /*      return, we are setting the indicator values to -128 to     */
       /*      return only a null value.                                  */
       /*   -  Commit or Rollback the inserts.                            */
       /*-----------------------------------------------------------------*/
     
    ext:
     
        rc = SQLGetSQLCA( henv, hdbc, hstmt, &sqlca ) ;
        if ( rc != SQL_SUCCESS ) printf( "RC = %d\n", rc ) ;
        memcpy( ca, &sqlca, sizeof( sqlca ) ) ;
        if ( output_sqlda != NULL ) {
           for ( cntr = 0; cntr < output_sqlda->sqld; cntr++ ) {
               if ( output_sqlda->sqlvar[cntr].sqlind != NULL )
                  *( output_sqlda->sqlvar[cntr].sqlind ) = -128 ;
           }
        }
     
        rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt ) ;
        CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
        rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_COMMIT ) ;
        CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
     
        printf( ">Disconnecting .....\n" ) ;
        rc = SQLDisconnect( hdbc ) ;
        CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
     
        rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
        CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
     
        rc = SQLFreeHandle( SQL_HANDLE_ENV,  henv ) ;
        if ( rc != SQL_SUCCESS )
            return( terminate( henv, rc ) ) ;
     
        return( SQL_SUCCESS ) ;
     
    }
     
    /* ... */
        SQLCHAR * stmt = "CALL inpsrv2(?, ?, ?, ?)" ;
    /* ... */
     
        rc = SQLPrepare( hstmt, stmt, SQL_NTS ) ;
        CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
        rc = SQLBindParameter( hstmt,
                               1,
                               SQL_PARAM_INPUT,
                               SQL_C_CHAR,
                               SQL_CHAR,
                               9,
                               0,
                               Tab_Name,
                               10,
                               NULL
                             ) ;
        CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
        rc = SQLBindParameter( hstmt,
                               2,
                               SQL_PARAM_INPUT,
                               SQL_C_CHAR,
                               SQL_CHAR,
                               10,
                               0,
                               Pres_Name[0],
                               11,
                               NULL
                             ) ;
        CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
        rc = SQLBindParameter( hstmt,
                               3,
                               SQL_PARAM_INPUT,
                               SQL_C_CHAR,
                               SQL_CHAR,
                               10,
                               0,
                               Pres_Name[1],
                               11,
                               NULL
                             ) ;
        CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
        rc = SQLBindParameter( hstmt,
                               4,
                               SQL_PARAM_INPUT,
                               SQL_C_CHAR,
                               SQL_CHAR,
                               10,
                               0,
                               Pres_Name[2],
                               11,
                               NULL
                             ) ;
        CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
        rc = SQLExecute( hstmt ) ;
        /* Ignore Warnings */
        if ( rc != SQL_SUCCESS_WITH_INFO )
           CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
     
      

  2.   

    大哥,这是一个静态SQL的例子,和动态SQL差异太大!
    我已经找到出错的位置了.
    是一条连接数据库的语句报错
    EXEC SQL CONNECT TO :dbname
    但是,我不清楚缺少什么东西,好象是一个头文件或者动态链接库.
      

  3.   

    EXEC SQL CONNECT TO sample USER :userid USING :passwd;
    你的dbname直接写可以吗?
      

  4.   

    我现在有遇到了新问题,我的预编译命令不好使了!
    前几次,我一直用PREP来进行编译,现在一用PREP 'E:\TEST.SQC'就报错.
    'DB21051E  当前环境不支持该命令。'