Creating an ODBC Application

ODBC and SQL Server

ODBC and SQL Server

Creating an ODBC Application

ODBC architecture has four components that perform the following functions.

Component Function
Application Calls ODBC functions to communicate with an ODBC data source, submits SQL statements, and processes result sets.
Driver Manager Manages communication between an application and all ODBC drivers used by the application.
Driver Processes all ODBC function calls from the application, connects to a data source, passes SQL statements from the application to the data source, and returns results to the application. If necessary, the driver translates ODBC SQL from the application to native SQL used by the data source.
Data source Contains all information a driver needs to access a specific instance of data in a DBMS.

An application that uses the ODBC interface to communicate with an instance of Microsoft® SQL Server™ performs the following tasks:

  • Connects with a data source

  • Sends SQL statements to the data source

  • Processes the results of statements from the data source

  • Processes errors and messages

  • Terminates the connection to the data source

A more complex application written for the SQL Server ODBC driver might also perform the following tasks:

  • Use cursors to control location in a result set

  • Request commit or rollback operations for transaction control

  • Perform distributed transactions involving two or more servers

  • Run stored procedures on the remote server

  • Call catalog functions to inquire about the attributes of a result set

  • Perform bulk copy operations

  • Manage long data (text, ntext, and image columns) operations

  • Control failover servers in case the primary server becomes unavailable

  • Log performance data and long-running queries

To make ODBC function calls, a C or C++ application must include the Sql.h, Sqlext.h, and Sqltypes.h header files. To make calls to the ODBC installer API functions, an application must include the Odbcinst.h header file. A Unicode ODBC application must include the Sqlucode.h header file. ODBC applications must be linked with the Odbc32.lib file. ODBC applications that call the ODBC installer API functions must be linked with the Odbccp32.lib file. By default, SQL Server Setup 2000 installs these header files into the C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Include directory and the library files into C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Lib when the SQL Server development tools are installed. The latest versions of these files can be downloaded with the latest Microsoft Data Access SDK from the Microsoft Web site.

Many ODBC drivers, including the SQL Server ODBC driver, offer driver-specific ODBC extensions. To take advantage of SQL Server ODBC driver-specific extensions, an application should include the Odbcss.h header file. This header file contains:

  • SQL Server ODBC driver-specific connection attributes.

  • SQL Server ODBC driver-specific statement attributes.

  • SQL Server ODBC driver-specific column attributes.

  • SQL Server-specific data types.

  • SQL Server-specific user-defined data types.

  • SQL Server ODBC driver-specific SQLGetInfo types.

  • SQL Server ODBC driver diagnostics fields.

  • SQL Server-specific diagnostic dynamic function codes.

  • C/C++ type definitions for SQL Server-specific native C data types (returned when columns bound to C data type SQL_C_BINARY).

  • Type definition for the SQLPERF data structure.

  • Bulk copy macros and prototypes to support bulk copy API usage through an ODBC connection.

  • Call the distributed query meta data API functions for lists of linked servers and their catalogs.

Any C or C++ ODBC application that uses the bulk copy feature of the SQL Server 2000 ODBC driver must be linked with the Odbcbcp.lib file. Applications calling the distributed query meta data API functions must also be linked with Odbcbcp.lib. The Odbcss.h and Odbcbcp.lib files are distributed as part of the SQL Server developer's tools. The SQL Server Include and Lib directories should be in the compiler's INCLUDE and LIB paths. If you have downloaded a version of the Microsoft Data Access SDK whose dates are later than the dates for SQL Server version 7.0, place the MSDA directories before the SQL Server 7.0 directories; for example:

LIB=c:\msdasdk\odbc\lib;C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Lib;c:\msdev\lib;c:\msdev\mfc\lib
INCLUDE=c:\msdasdk\odbc\include;C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Include;c:\msdev\include;
c:\msdev\mfc\include

One design decision made early in the process of building an application is whether the application needs to have multiple ODBC calls outstanding at the same time. There are two methods for supporting multiple concurrent ODBC calls:

  • ODBC asynchronous mode

  • Multithreading