ODBC Auto-Commit Off

Go back to: Code Samples
/* For conciseness, this code omits error checking */
 
/* Allocate a statement handle */
rc = SQLAllocStmt((SQLHDBC)hdbc, (SQLHSTMT *)&hstmt);
 
/* Turn auto-commit off */
rc = SQLSetConnectAttr(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
 
/* Prepare an INSERT statement for multiple executions */
strcpy(sqlStatement, "INSERT INTO employees VALUES (?, ?, ?)");
rc = SQLPrepare((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Bind parameters */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
                      SQL_C_SLONG, SQL_INTEGER, 10, 0,
                      &id, sizeof(id), NULL);
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT,
                      SQL_C_CHAR, SQL_CHAR, 20, 0,
                      name, sizeof(name), NULL);
rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT,
                      SQL_C_SLONG, SQL_INTEGER, 10, 0,
                      &salary, sizeof(salary), NULL);
 
/* Set parameter values before execution */
id = 20;
strcpy(name,"Employee20");
salary = 100000;
rc = SQLExecute(hstmt);
 
/* Change parameter values for the next execution */
id = 21;
strcpy(name,"Employee21");
salary = 150000;
rc = SQLExecute(hstmt);
 
/* Reset parameter bindings */
rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
 
/* Manual commit */
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);            
 
/* Execute a SELECT statement. A prepare is unnecessary
   because it’s only executed once. */
strcpy(sqlStatement, "SELECT id, name, salary FROM employees");
rc = SQLExecDirect((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Fetch the first row */
rc = SQLFetch(hstmt);
while (rc != SQL_NO_DATA_FOUND) { 
 
/* All rows are returned when fetch returns SQL_NO_DATA_FOUND */
 
   /* Get the data for each column in the result set row */
   rc = SQLGetData(hstmt, 1, SQL_C_LONG, &id,
                   sizeof(id), NULL);
   rc = SQLGetData(hstmt, 2, SQL_C_CHAR, &name,
                   sizeof(name), NULL);
   rc = SQLGetData(hstmt, 3, SQL_C_LONG, &salary,
                   sizeof(salary), NULL);
   printf("\nID: %d Name: %s Salary: %d", id, name, salary);
 
   /* Fetch the next row of data */
   rc = SQLFetch(hstmt);
   }
 
/* Close the cursor */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_CLOSE);
 
strcpy(sqlStatement,
       "UPDATE employees SET salary = salary * 1.05 WHERE id = ?");
 
/* Prepare the UPDATE statement for multiple executions */
rc = SQLPrepare((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Bind parameter */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
                      SQL_C_SLONG, SQL_INTEGER, 10, 0,
                      &index, sizeof(index), NULL);
 
for (index = 0; index < 10; index++) {
 
   /* Execute the UPDATE statement for each
      value of index between 0 and 9 */
   rc = SQLExecute(hstmt);
   }
 
/* Manual commit */
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
 
/* Reset parameter bindings */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_RESET_PARAMS);
 
/* Execute a SELECT statement. A prepare is unnecessary
   because it’s only executed once. */
strcpy(sqlStatement, "SELECT id, name, salary FROM employees");
rc = SQLExecDirect ((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Fetch the first row */
rc = SQLFetch(hstmt);
while (rc != SQL_NO_DATA_FOUND) { 
 
/* All rows are returned when fetch returns SQL_NO_DATA_FOUND */
 
   /* Get the data for each column in the result set row */
   rc = SQLGetData(hstmt, 1, SQL_C_LONG, &id,
                   sizeof(id), NULL);
   rc = SQLGetData(hstmt, 2, SQL_C_CHAR, &name,
                   sizeof(name), NULL);
   rc = SQLGetData(hstmt,3,SQL_C_LONG,&salary,
                   sizeof(salary), NULL);
   printf("\nID: %d Name: %s Salary: %d", id, name, salary);
 
   /* Fetch the next row of data */
   rc = SQLFetch(hstmt);
   }
 
/* Close the cursor */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_CLOSE);
 
/* Manual commit */
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);



Like what you see? Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis

Book Content Copyright © 2009 by Prentice Hall PTR. All rights reserved. | Corporate Sponsor DataDirect Technologies.