#include #include #include #include #include void print_error( SQLSMALLINT htype, /* A handle type identifier */ SQLHANDLE hndl, /* A handle */ SQLRETURN frc, /* Return code to be included with error msg */ int line, /* Used for output message, indcate where */ char * file /* the error was reported from */ ) { SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1] ; SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1] ; SQLINTEGER sqlcode ; SQLSMALLINT length, i ; SQLINTEGER NumRecords; printf( ">--- ERROR -- RC = %d Reported from %s, line %d ------------\n", frc, file, line ) ; SQLGetDiagField(htype, hndl, 0,SQL_DIAG_NUMBER, &NumRecords, SQL_IS_INTEGER,NULL); printf("Total Number of diagnostic records: %d\n",NumRecords); i = 1 ; while ( SQLGetDiagRec( htype, hndl, i, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length ) == SQL_SUCCESS ) { printf( " SQLSTATE: %s\n", sqlstate ) ; printf( "Native Error Code: %ld\n", sqlcode ) ; printf( "%s \n", buffer ) ; i++ ; } printf( ">--------------------------------------------------\n" ) ; } int main() { // Declare The Local Memory Variables SQLHANDLE EnvHandle = 0; SQLHANDLE ConHandle = 0; SQLHANDLE StmtHandle = 0; SQLRETURN RetCode = SQL_SUCCESS; SQLCHAR SQLStmt[255]; SQLCHAR JobType[10]; SQLCHAR EmpNo[10]; SQLCHAR LastName[25]; SQLCHAR FirstName[15]; int EmpId=1002; /** * INITIALIZATION **/ // Allocate An Environment Handle SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,&EnvHandle); // Set The ODBC Application Version To 3.x printf("Setting the ODBC version... \n"); if (EnvHandle != 0) SQLSetEnvAttr(EnvHandle, SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER); // Allocate A Connection Handle printf("Creating Connection handle... \n"); if (EnvHandle != 0) SQLAllocHandle(SQL_HANDLE_DBC, EnvHandle,&ConHandle); // Connect To The Appropriate Data Source if (ConHandle != 0) { RetCode = SQLConnect(ConHandle, (SQLCHAR *) "SAMPLE",SQL_NTS, (SQLCHAR *) "app",SQL_NTS, (SQLCHAR *) "app", SQL_NTS); printf("Got the connection... \n"); } /* * TRANSACTION PROCESSING **/ // Allocate An SQL Statement Handle if (ConHandle != 0 && RetCode == SQL_SUCCESS) SQLAllocHandle(SQL_HANDLE_STMT, ConHandle,&StmtHandle); else{ printf("Error getting connection:\n"); print_error((SQLSMALLINT)SQL_HANDLE_DBC,ConHandle, RetCode,__LINE__,__FILE__ ); return(0); } // Define A SELECT SQL Statement That Uses A Parameter strcpy((char *) SQLStmt, "INSERT INTO EMPLOYEE VALUES (?,?,?,?, \ CURRENT DATE)"); RetCode = SQLPrepare(StmtHandle, SQLStmt, SQL_NTS); if(RetCode!=SQL_SUCCESS) { printf("Error preparing the insert statement:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } RetCode = SQLBindParameter(StmtHandle, 1,SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,sizeof(EmpId), 0, &EmpId ,sizeof(EmpId), NULL); if(RetCode!=SQL_SUCCESS) { printf("Error binding the first param in insert:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } RetCode = SQLBindParameter(StmtHandle, 2,SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,sizeof(FirstName), 0, FirstName,sizeof(FirstName), NULL); if(RetCode!=SQL_SUCCESS) { printf("Error binding the second param in insert:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } RetCode = SQLBindParameter(StmtHandle, 3,SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,sizeof(LastName), 0, LastName,sizeof(LastName), NULL); if(RetCode!=SQL_SUCCESS) { printf("Error binding the third param in insert:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } RetCode = SQLBindParameter(StmtHandle, 4,SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,sizeof(JobType), 0, JobType,sizeof(JobType), NULL); if(RetCode!=SQL_SUCCESS) { printf("Error binding the fourth param in insert:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } strcpy((char *) FirstName, "Robert"); strcpy((char *) LastName, "Evans"); strcpy((char *) JobType, "ENGINEER"); RetCode = SQLExecute(StmtHandle); if (RetCode == SQL_SUCCESS) { printf("Successfully executed the insert statement...\n"); } else { printf("Error executing insert statement:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } // A SELECT Statement strcpy((char *) SQLStmt, "SELECT EMPNO, LASTNAME FROM "); strcat((char *) SQLStmt, "EMPLOYEE WHERE JOBTYPE = ?"); // Prepare The SQL Statement RetCode = SQLPrepare(StmtHandle, SQLStmt, SQL_NTS); if(RetCode!=SQL_SUCCESS) { printf("Error preparing:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } // Bind The Parameter Marker Used In The SQL Statement To // An Application Variable RetCode = SQLBindParameter(StmtHandle, 1,SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,sizeof(JobType), 0, JobType,sizeof(JobType), NULL); if(RetCode!=SQL_SUCCESS) { printf("Error binding:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } // Populate The "Bound" Application Variable strcpy((char *) JobType, "ENGINEER"); // Execute The SQL Statement RetCode = SQLExecute(StmtHandle); // If The SQL Statement Executed Successfully, Retrieve // The Results if (RetCode == SQL_SUCCESS) { printf("Successfully executed the select statement...\n"); // Bind The Columns In The Result Data Set Returned // To Application Variables SQLBindCol(StmtHandle, 1, SQL_C_CHAR, (SQLPOINTER)EmpNo, sizeof(EmpNo), NULL); SQLBindCol(StmtHandle, 2, SQL_C_CHAR, (SQLPOINTER) LastName, sizeof(LastName), NULL); // While There Are Records In The Result Data Set // Produced, Retrieve And Display Them while (RetCode != SQL_NO_DATA) { RetCode = SQLFetch(StmtHandle); if (RetCode != SQL_NO_DATA) printf("%-8s %s\n", EmpNo, LastName); } }else { printf("Error executing select statement:\n"); print_error((SQLSMALLINT)SQL_HANDLE_STMT,StmtHandle, RetCode,__LINE__,__FILE__ ); } // Commit The Transaction RetCode = SQLEndTran(SQL_HANDLE_DBC, ConHandle,SQL_COMMIT); /** * TERMINATION **/ // Free The SQL Statement Handle if (StmtHandle != 0) SQLFreeHandle(SQL_HANDLE_STMT, StmtHandle); // Terminate The Data Source Connection if (ConHandle != 0) RetCode = SQLDisconnect(ConHandle); // Free The Connection Handle if (ConHandle != 0) SQLFreeHandle(SQL_HANDLE_DBC, ConHandle); // Free The Environment Handle if (EnvHandle != 0) SQLFreeHandle(SQL_HANDLE_ENV, EnvHandle); // Return Control To The OS return(0); }