描述参数Describing Parameters

SQLBindParameter具有描述参数的参数:其 SQL 类型、精度和小数位数。SQLBindParameter has arguments that describe the parameter: its SQL type, precision, and scale. 驱动程序使用此信息或元数据将参数值转换为数据源所需的类型。The driver uses this information, or metadata, to convert the parameter value to the type needed by the data source. 乍一看,驱动程序的位置可能与应用程序的参数元数据的位置更好;毕竟,驱动程序可以轻松发现结果集列的元数据。At first glance, it might seem that the driver is in a better position to know the parameter metadata than the application; after all, the driver can easily discover the metadata for a result set column. 事实证明,事实并非如此。As it turns out, this is not the case. 首先,大多数数据源不提供驱动程序发现参数元数据的方法。First, most data sources do not provide a way for the driver to discover parameter metadata. 其次,大多数应用程序已经知道了元数据。Second, most applications already know the metadata.

如果在应用程序中硬编码 SQL 语句,则应用程序编写器已经知道每个参数的类型。If an SQL statement is hard-coded in the application, the application writer already knows the type of each parameter. 如果 SQL 语句是在运行时由应用程序构造的,则应用程序可以在生成语句时确定元数据。If an SQL statement is constructed by the application at run time, the application can determine the metadata as it builds the statement. 例如,当应用程序构造子句时For example, when the application constructs the clause

WHERE OrderID = ?  

它可以为 "订单 Id" 列调用SQLColumnsit can call SQLColumns for the OrderID column.

应用程序无法轻松确定参数元数据的唯一情况是用户输入参数化语句时。The only situation in which the application cannot easily determine the parameter metadata is when the user enters a parameterized statement. 在这种情况下,应用程序会调用SQLPrepare来准备语句, SQLNumParams来确定参数的数目,并调用SQLDescribeParam来描述每个参数。In this case, the application calls SQLPrepare to prepare the statement, SQLNumParams to determine the number of parameters, and SQLDescribeParam to describe each parameter. 但是,如前所述,大多数数据源不提供驱动程序发现参数元数据的方法,因此SQLDescribeParam不受广泛支持。However, as was noted earlier, most data sources do not provide a way for the driver to discover parameter metadata, so SQLDescribeParam is not widely supported.