ODBC SQLExecDirect

#include
#include
#include
#include
#include
#include
#include

void CheckReturnCode(SQLRETURN rc, SQLHENV henv,SQLHDBC hdbc, SQLHSTMT hstmt,char* msg, char *filename, int lineno);

void main( void )
{
     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 szConnOut[255]; /* Buffer for completed connection string */
     SQLSMALLINT cbConnOut; /* number of bytes returned in szConnOut */
     SQLLEN nRowCount; /* How many row was uupdated */
     SQLCHAR *szConnString = (SQLCHAR *)"DSN=TT41Data"; /* Connection attributes, please set uid and pwd in $ODBCINI file */
     char sqlString[255];

     SQLINTEGER v2;
     SQLLEN v2size;
     SQLCHAR v3[256];
     SQLLEN v3size;
     hrtime_t start, end;

     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 allocate connection handle/n", __FILE__, __LINE__);

     rc = SQLDriverConnect(hDbc, NULL,szConnString, SQL_NTS,szConnOut, 255,&cbConnOut,SQL_DRIVER_NOPROMPT);
     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 */

     /* create table tab(v1 int, v2 int, v3 varchar(10)) */

     sprintf(sqlString,"insert into tab values(1,2,'123')");
     rc = SQLExecDirect(hStmt, (SQLCHAR*)sqlString,SQL_NTS);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to insert /n", __FILE__,__LINE__);

     sprintf(sqlString,"update tab set v2=3,v3='456' where v1=1");
     rc = SQLExecDirect(hStmt, (SQLCHAR*)sqlString,SQL_NTS);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to update/n", __FILE__,__LINE__);

     sprintf(sqlString,"select v2, v3 from tab where v1=1");
     SQLBindCol(hStmt,1,SQL_C_LONG, &v2,200,&v2size);
     SQLBindCol(hStmt,2,SQL_C_CHAR,  &v3,200,&v3size);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to bind/n", __FILE__,__LINE__);
     rc = SQLExecDirect(hStmt, (SQLCHAR*)sqlString,SQL_NTS);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to select/n", __FILE__,__LINE__);
     rc = SQLFetch(hStmt);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to fetch/n", __FILE__,__LINE__);

     printf("select value v2=%d,v3=%s/n",v2,v3);

     /*******************************************************************************/
     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  free 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 && rc != SQL_SUCCESS_WITH_INFO ) {
          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);
               switch (ret) {
               case SQL_SUCCESS:
                    fprintf(stderr, "*** %s/n*** ODBC Error/Warning = %s, TimesTen Error/Warning = %d/n",szErrorMsg, szSqlState,pfNativeError);
                    break;
               case SQL_SUCCESS_WITH_INFO:
                    fprintf(stderr, "*** Call to SQLError failed with return code of SQL_SUCCESS_WITH_INFO./n *** Need to increase size of message buffer./n");
                    break;
               case SQL_INVALID_HANDLE:
                    fprintf(stderr, "*** Call to SQLError failed with return code of SQL_INVALID_HANDLE./n");
                    break;
               case SQL_ERROR:
                    fprintf(stderr, "*** Call to SQLError failed with return code of SQL_ERROR./n");
                    break;
               case SQL_NO_DATA_FOUND:
                    break;
               default:
                    break;
               } /* switch */
          } /* while */
     }
}

你可能感兴趣的:(ODBC,sql,null,file,attributes,insert,buffer)