绑定参数标记Binding Parameter Markers

应用程序通过调用SQLBindParameter绑定参数。The application binds parameters by calling SQLBindParameter. SQLBindParameter一次绑定一个参数。SQLBindParameter binds one parameter at a time. 在此应用程序中,应用程序指定了以下内容:With it, the application specifies the following:

  • 参数编号。The parameter number. 参数在 SQL 语句中以递增参数顺序编号,以数字1开头。Parameters are numbered in increasing parameter order in the SQL statement, starting with the number 1. 虽然指定的参数号大于 SQL 语句中参数的数目是合法的,但在执行该语句时将忽略参数值。While it is legal to specify a parameter number that is higher than the number of parameters in the SQL statement, the parameter value will be ignored when the statement is executed.

  • 参数类型(输入、输入/输出或输出)。The parameter type (input, input/output, or output). 除了过程调用中的参数,所有参数都是输入参数。Except for parameters in procedure calls, all parameters are input parameters. 有关详细信息,请参阅本部分后面的过程参数For more information, see Procedure Parameters, later in this section.

  • 绑定到参数的变量的 C 数据类型、地址和字节长度。The C data type, address, and byte length of the variable bound to the parameter. 驱动程序必须能够将数据从 C 数据类型转换为 SQL 数据类型,否则将返回错误。The driver must be able to convert the data from the C data type to the SQL data type or an error is returned. 有关支持的转换的列表,请参阅附录 D:数据类型中的将数据从 C 转换为 SQL 数据类型For a list of supported conversions, see Converting Data from C to SQL Data Types in Appendix D: Data Types.

  • 参数本身的 SQL 数据类型、精度和小数位数。The SQL data type, precision, and scale of the parameter itself.

  • 长度/指示器缓冲区的地址。The address of a length/indicator buffer. 它提供二进制或字符数据的字节长度,指定数据为 NULL,或指定将数据与SQLPutData一起发送。It provides the byte length of binary or character data, specifies that the data is NULL, or specifies that the data will be sent with SQLPutData. 有关详细信息,请参阅使用长度/指示器值For more information, see Using Length/Indicator Values.

例如,以下代码将销售人员CustID绑定到业务员和 CustID 列的参数。For example, the following code binds SalesPerson and CustID to parameters for the SalesPerson and CustID columns. 由于销售人员包含字符数据(可变长度),因此代码指定了销售人员的字节长度(11),并绑定SalesPersonLenOrInd以包含销售人员的数据字节长度。Because SalesPerson contains character data, which is variable length, the code specifies the byte length of SalesPerson (11) and binds SalesPersonLenOrInd to contain the byte length of the data in SalesPerson. CustID不需要此信息,因为它包含固定长度的整数数据。This information is not necessary for CustID because it contains integer data, which is of fixed length.

SQLCHAR       SalesPerson[11];  
SQLINTEGER    SalesPersonLenOrInd, CustIDInd;  
SQLUINTEGER   CustID;  
  
// Bind SalesPerson to the parameter for the SalesPerson column and  
// CustID to the parameter for the CustID column.  
SQLBindParameter(hstmt1, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,  
                  SalesPerson, sizeof(SalesPerson), &SalesPersonLenOrInd);  
SQLBindParameter(hstmt1, 2, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 10, 0,  
                  &CustID, 0, &CustIDInd);  
  
// Set values of salesperson and customer ID and length/indicators.  
strcpy_s((char*)SalesPerson, _countof(SalesPerson), "Garcia");  
SalesPersonLenOrInd = SQL_NTS;  
CustID = 1331;  
CustIDInd = 0;  
  
// Execute a statement to get data for all orders made to the specified  
// customer by the specified salesperson.  
SQLExecDirect(hstmt1,"SELECT * FROM Orders WHERE SalesPerson=? AND CustID=?",SQL_NTS);  

调用SQLBindParameter时,驱动程序会将此信息存储在语句的结构中。When SQLBindParameter is called, the driver stores this information in the structure for the statement. 执行语句时,它将使用该信息检索参数数据并将其发送到数据源。When the statement is executed, it uses the information to retrieve the parameter data and send it to the data source.

备注

在 ODBC 1.0 中,参数与SQLSetParam绑定。In ODBC 1.0, parameters were bound with SQLSetParam. 驱动程序管理器根据应用程序和驱动程序使用的 ODBC 版本来映射SQLSetParamSQLBindParameter之间的调用。The Driver Manager maps calls between SQLSetParam and SQLBindParameter, depending on the versions of ODBC used by the application and driver.