Collection Contents Index Introduction to ODBC programming Error checking in ODBC pdf/chap4.pdf

Programming Interfaces Guide
   CHAPTER 4. ODBC Programming     

ODBC fundamentals


Database access in ODBC is carried out using SQL statements passed as strings to ODBC functions.

The following fundamental objects are used for every ODBC program. Each object is referenced by a handle.

All access to these objects is through function calls; the application cannot directly access any information about the object from its handle. In the Windows and Windows NT environments, all the function calls are described in full detail in the ODBC API help file, which is part of the ODBC SDK.

Top of page  Compiling and linking an ODBC application

Every C source file using ODBC functions must include one of the following lines:

These files all include the main ODBC include file odbc.h, which defines all of the functions, data types and constant definitions required to write an ODBC program. The file odbc.h and the environment-specific include files are installed in the h subdirectory of your Adaptive Server Anywhere installation directory.

Once your program has been compiled, you must link with the appropriate import library file to have access to the ODBC functions:

Top of page  A first example

The following is a simple ODBC program:

{
   HENV   env;
   HDBC   dbc;
   HSTMT   stmt;
   
   SQLAllocEnv( &env );
   SQLAllocConnect( env, &dbc );
   SQLConnect( dbc, "asademo", SQL_NTS,
               "dba", SQL_NTS, "sql", SQL_NTS );
   SQLSetConnectOption( dbc, SQL_AUTOCOMMIT, FALSE );
   SQLAllocStmt( dbc, &stmt );
   
   /* Delete all the order items for order 2015 */
   SQLExecDirect( stmt,
      "DELETE FROM sales_order_items WHERE id=2015",
       SQL_NTS );
   
   /* Use rollback to undo the delete */
   SQLTransact( env, dbc, SQL_ROLLBACK );
   SQLFreeStmt( stmt, SQL_DROP );
   SQLDisconnect( dbc );
   SQLFreeConnect( dbc );
   SQLFreeEnv( env );
}

Notes 

Top of page  Threads in ODBC applications

You can develop multi-threaded ODBC applications for Adaptive Server Anywhere. It is recommended that you use a separate connection for each thread.

You can use a single connection for multiple threads. However, the database server does not allow more than one active request for any one connection at a time. If one thread executes a statement that takes a long time, all other threads must wait until the request is complete.

Top of page  

Collection Contents Index Introduction to ODBC programming Error checking in ODBC pdf/chap4.pdf