ODBC 中的 C 数据类型C Data Types in ODBC

ODBC 定义应用程序变量和其对应的类型标识符所使用的 C 数据类型。ODBC defines the C data types that are used by application variables and their corresponding type identifiers. 这些使用绑定到结果集列和语句参数的缓冲区。These are used by the buffers that are bound to result set columns and statement parameters. 例如,假设应用程序想要从以字符格式的结果集列中检索数据。For example, suppose an application wants to retrieve data from a result set column in character format. 它声明的变量 SQLCHAR * 数据类型,并将此变量绑定到结果集列具有其类型标识符为 SQL_C_CHAR。It declares a variable with the SQLCHAR * data type and binds this variable to the result set column with a type identifier of SQL_C_CHAR. C 数据类型和类型标识符的完整列表,请参阅附录 d:数据类型For a complete list of C data types and type identifiers, see Appendix D: Data Types.

ODBC 还定义从每个 SQL 数据类型为 C 数据类型的默认映射。ODBC also defines a default mapping from each SQL data type to a C data type. 例如,数据源中的 2 字节整数映射到应用程序中的 2 字节整数。For example, a 2-byte integer in the data source is mapped to a 2-byte integer in the application. 若要使用的默认映射,应用程序,请指定 SQL_C_DEFAULT 类型标识符。To use the default mapping, an application specifies the SQL_C_DEFAULT type identifier. 但是,建议不要使用此标识符的互操作性的原因。However, use of this identifier is discouraged for interoperability reasons.

在 ODBC 中定义的所有整数 C 数据类型1.x是否已进行签名。All integer C data types defined in ODBC 1.x were signed. ODBC 2.0 添加了无符号的 C 数据类型和其对应的类型标识符。Unsigned C data types and their corresponding type identifiers were added in ODBC 2.0. 因此,应用程序和驱动程序需要处理时特别小心1.x版本。Because of this, applications and drivers need to be particularly careful when dealing with 1.x versions.

C 数据类型扩展能力C Data Type Extensibility

在 ODBC 3.8,可以指定特定于驱动程序的 C 数据类型。In ODBC 3.8, you can specify driver-specific C data types. 这使您可以将 SQL 类型绑定为 ODBC 应用程序中的特定于驱动程序的 C 类型,当您调用SQLBindColSQLGetData,或SQLBindParameterThis enables you to bind a SQL type as a driver-specific C type in ODBC applications when you call SQLBindCol, SQLGetData, or SQLBindParameter. 这可用于支持新的服务器类型,因为现有的 C 数据类型不能正确表示新的服务器数据类型。This can be useful for supporting new server types, because existing C data types might not correctly represent the new server data types. 使用特定于驱动程序的 C 类型可以提高驱动程序可以执行的转换数。Using driver-specific C types can increase the number of conversions that drivers can perform.

例如,假设数据库管理系统 (DBMS) 引入了新的 SQL 类型, DATETIMEOFFSET,用于表示的日期和时间与时区信息。For example, suppose a database management system (DBMS) introduced a new SQL type, DATETIMEOFFSET, to represent the date and time with time zone information. 对应于 ODBC 中会有任何特定的 C 类型DATETIMEOFFSETThere would be no specific C type in ODBC that corresponded to DATETIMEOFFSET. 应用程序必须将绑定DATETIMEOFFSET SQL_C_BINARY 和强制转换为其用户定义数据类型。An application would have to bind DATETIMEOFFSET as SQL_C_BINARY and cast it to a user-defined data type. 开始在 ODBC 3.8 中 C 数据类型扩展能力,驱动程序可以定义新的相应 C 类型。Beginning in ODBC 3.8 with C data type extensibility, a driver can define a new corresponding C type. 例如,对于新的 SQL 类型 DATETIMEOFFSET,驱动程序可以定义如 SQL_C_DATETIMEOFFSET 对应的新 C 类型。For example, for the new SQL type DATETIMEOFFSET, the driver can define a new corresponding C type such as SQL_C_DATETIMEOFFSET. 然后,应用程序可以将新的 SQL 类型绑定为特定于驱动程序的 C 类型。Then, an application can bind the new SQL type as a driver-specific C type.

C 数据类型定义驱动程序中,如下所示:A C data type is defined in the driver as follows:

  • 应用程序、 ODBC 驱动程序和驱动程序管理器的 ODBC 符合性级别是 3.8 (或更高版本)。The ODBC compliance level for an application, ODBC driver, and Driver Manager is 3.8 (or higher).

  • 0x4000 和 0x7FFF 之间是特定于驱动程序的 C 类型的数据范围。The data range of a driver-specific C type is between 0x4000 and 0x7FFF.

  • 该驱动程序定义的 C 类型相对应的数据的结构。The driver defines the structure of the data corresponding to the C type. 进行这种特定于驱动程序的 SDK。This can be done in the driver-specific SDK.

驱动程序管理器将不会验证 0x4000 和 0x7FFF; 的范围中定义的 C 类型该驱动程序将执行验证和任何数据类型转换。The driver manager will not validate a C type defined in the range of 0x4000 and 0x7FFF; the driver will perform the validation and any data type conversion. 但如果传递给驱动程序管理器的 C 类型的数据范围之间 0x8000 和 0xFFFF 或 0x0000 和 0x3FFF 之间,驱动程序管理器将验证的 C 数据类型。But if the data range of a C type passed to the driver manager is between 0x0000 and 0x3FFF or between 0x8000 and 0xFFFF, the driver manager will validate the C data type.

备注

特定于驱动程序的 C 数据类型应驱动程序文档中所述。Driver-specific C data types should be described in the driver documentation.

若要指定 ODBC 3.8 的符合性级别,应用程序调用SQLSetEnvAttr SQL_ATTR_ODBC_VERSION 与属性设置为SQL_OV_ODBC3_80To specify an ODBC compliance level of 3.8, an application calls SQLSetEnvAttr with the SQL_ATTR_ODBC_VERSION attribute set to SQL_OV_ODBC3_80. 若要确定版本的驱动程序,应用程序调用SQLGetInfo SQL_DRIVER_ODBC_VER 使用。To determine the version of the driver, an application calls SQLGetInfo with SQL_DRIVER_ODBC_VER.

有关 ODBC 3.8 详细信息,请参阅What's New in ODBC 3.8For more information about ODBC 3.8, see What's New in ODBC 3.8.

请参阅See Also

C 数据类型C Data Types