- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
ODBC API Basics
Before you get into the various functions provided by the ODBC API, let's look at a few key concepts used throughout the API. In this section, you will look at allocating various handles that are used by ODBC, data types used in ODBC, and how ODBC passes error information to your application.
ODBC Handles
The ODBC API introduces new handle types that are used to reference information about your app's ODBC environment, specific database connections, SQL statements, and Descriptors. In ODBC 3, each of these handle types is allocated with a single function—SQLAllocHandle()—and freed with a single function—SQLFreeHandle().
The SQLAllocHandle() function allocates the internal structures for the various handle types:
SQLRETURN SQLAllocHandle(SQL_SMALLINT HandleType, SQLHANDLE InputHandle,
SQLHANDLE * OutputHandlePtr);
The HandleType parameter can be one of the following values:
- SQL_HANDLE_ENV
- SQL_HANDLE_DBC
- SQL_HANDLE_STMT
- SQL_HANDLE_DESC
These values determine which sort of handle is being allocated. You will look at specific examples of allocating each type when you learn about allocating environment, connection, statement, and descriptor handles.
The InputHandle specifies the handle from which the new handle is derived. For example, a connection handle is allocated based on an environment handle. An environment handle, however, isn't derived from another handle, so InputHandle should be set to SQL_NULL_HANDLE.
The OutputHandlePtr should point to the new handle that is to be allocated.
If the new handle is successfully allocated, SQlAllocHandle() will return SQL_SUCCESS; otherwise, it will return SQL_ERROR. You will look at how to handle ODBC errors in just a bit.
When you are finished with a particular ODBC handle in your application, you should free the structures associated with the handle by calling SQLFreeHandle(), which takes the type of handle to free and the actual handle that is to be freed.
ODBC Data Types
ODBC defines many different standard data types that are used in ODBC applications. There are C data types, which are used in your application code, and SQL data types, which are used to describe the type of data that is used within a data source. Many of the ODBC calls that move data from the application to the data source, or vice versa, can automatically perform conversion between many of these types.
Some of the most common C data types defined in ODBC are listed in Table 20.1, although there are many others described in the ODBC 3 Programmer's Reference. The C type identifier is a constant used to represent the type, the ODBC typedef gives the type you should use in declaring this type of variable, and the C type shows what the preprocessor will resolve this to.
Table 20.1. ODBC C Data Types
| C Type Identifier | ODBC C Typedef | C Type |
| SQL_C_CHAR | SQLCHAR * | unsigned char * |
| SQL_C_SSHORT | SQLSMALLINT | short int |
| SQL_C_USHORT | SQLUSMALLINT | unsigned short int |
| SQL_C_SLONG | SQLINTEGER | long int |
| SQL_C_ULONG | SQLUINTEGER | unsigned long int |
| SQL_C_FLOAT | SQLREAL | float |
| SQL_C_DOUBLE | SQLDOUBLE | double |
| SQL_C_BIT | SQLCHAR | unsigned char |
| SQL_C_STINYINT | SQLSCHAR | signed char |
| SQL_C_UTINYINT | SQLCHAR | unsigned char |
| SQL_C_SBIGINT | SQLBIGINT | _int64 |
| SQL_C_UBIGINT | SQLUBIGINT | unsigned _int64 |
| SQL_C_BINARY | SQLCHAR * | unsigned char * |
| SQL_C_VARBOOKMARK | SQLCHAR * | unsigned char * |
ODBC uses SQL data types to describe the data types that are stored in the database. Table 20.2 shows some of the more common types, listing the constant SQL type identifier that is used in your apps and an example SQL definition for the type. For a complete listing, see the ODBC 3 Programmer's Reference.
Table 20.2. ODBC SQL Data Types
| SQL Type Identifier | Sample SQL Definition |
| SQL_CHAR | CHAR(n) |
| SQL_VARCHAR | VARCHAR(n) |
| SQL_LONGVARCHAR | LONG VARCHAR |
| SQL_DECIMAL | DECIMAL(p,s) |
| SQL_NUMERIC | NUMERIC(p,s) |
| SQL_SMALLINT | SMALLINT |
| SQL_INTEGER | INTEGER |
| SQL_REAL | REAL |
| SQL_FLOAT | FLOAT(p) |
| SQL_DOUBLE | DOUBLE PRECISION |
| SQL_BIT | BIT |
| TINYINT | TINYINT |
| SQL_BIGINT | BIGINT |
| SQL_BINARY | BINARY(n) |
| SQL_VARBINARY | VARBINARY(n) |
| SQL_LONGVARBINARY | LONG VARBINARY |
| SQL_TYPE_DATE | DATE |
| SQL_TYPE_TIME | TIME(p) |
Creating ODBC Applications | Next Section

Account Sign In
View your cart