Access Cycle of ODBC Functions

To perform a SQL command in an ODBC application, the following sequence of ODBC operations must be executed:

  1. Connect to the data source (ODBC functions SQLAllocEnv,SQLAllocConnect, SQLConnect)
  2. Allocate a cursor/statement handle (ODBC function SQLAllocStmt)
  3. Prepare the SQL command (ODBC function SQLPrepare, orSQLExecDirect for direct prepare and execute)
  4. Bind program data to SQL data (ODBC functions SQLBindParameter for binding parameter markers to program data and SQLBindCol for binding selected columns to program data)
  5. Execute the SQL command (ODBC function SQLExecute orSQLExecDirect for direct prepare and execute)
  6. Fetch rows if the SQL command is a select (ODBC function SQLFetch or SQLExtendedFetch for scrollable cursors)
  7. Commit or roll back the transaction (ODBC function SQLTransact)
  8. Free the cursor/statement handle (ODBC function SQLFreeStmt)
  9. Disconnect from the database (ODBC functions SQLDisconnect, SQLFreeConnect, SQLFreeEnv)

The main steps in performing a SQL command are: connect to the database, prepare the SQL command, execute the SQL command, fetch the results if it is a SQL select command, and disconnect from the database.

To perform a SQL command repeatedly or to perform multiple SQL commands, it is not necessary (and not appropriate) to execute all operations listed above every time you want to perform a SQL command. The sequence of the ODBC functions for performing SQL commands repeatedly is called the access cycle of ODBC functions.