SQLPutData 函数SQLPutData Function

符合性Conformance
版本引入了:ODBC 1.0 标准符合性:ISO 92Version Introduced: ODBC 1.0 Standards Compliance: ISO 92

摘要Summary
SQLPutData ,应用程序可以将参数或列的数据发送到在语句执行时驱动程序。SQLPutData allows an application to send data for a parameter or column to the driver at statement execution time. 此函数可用于将部分中的字符或二进制数据值发送到字符、 二进制或数据源特定的数据类型 (例如,SQL_LONGVARBINARY 或 SQL_LONGVARCHAR 类型参数) 的列。This function can be used to send character or binary data values in parts to a column with a character, binary, or data source-specific data type (for example, parameters of the SQL_LONGVARBINARY or SQL_LONGVARCHAR types). SQLPutData支持绑定到 Unicode 的 C 数据类型,即使基础驱动程序不支持 Unicode 数据。SQLPutData supports binding to a Unicode C data type, even if the underlying driver does not support Unicode data.

语法Syntax

  
SQLRETURN SQLPutData(  
      SQLHSTMT     StatementHandle,  
      SQLPOINTER   DataPtr,  
      SQLLEN       StrLen_or_Ind);  

参数Arguments

StatementHandleStatementHandle
[输入]语句句柄。[Input] Statement handle.

DataPtrDataPtr
[输入]指向包含参数或列的实际数据的缓冲区的指针。[Input] Pointer to a buffer containing the actual data for the parameter or column. 中指定的 C 数据类型必须是数据ValueType的参数SQLBindParameter (适用于参数数据) 或TargetType自变量的SQLBindCol (适用于列数据)。The data must be in the C data type specified in the ValueType argument of SQLBindParameter (for parameter data) or the TargetType argument of SQLBindCol (for column data).

StrLen_or_IndStrLen_or_Ind
[输入]长度* DataPtr[Input] Length of *DataPtr. 指定对的调用中发送的数据量SQLPutDataSpecifies the amount of data sent in a call to SQLPutData. 数据量可以随每个调用中给定的参数或列。The amount of data can vary with each call for a given parameter or column. StrLen_or_Ind将忽略未满足以下条件之一:StrLen_or_Ind is ignored unless it meets one of the following conditions:

  • StrLen_or_Ind是 SQL_NTS、 SQL_NULL_DATA 或 SQL_DEFAULT_PARAM。StrLen_or_Ind is SQL_NTS, SQL_NULL_DATA, or SQL_DEFAULT_PARAM.

  • 中指定的 C 数据类型SQLBindParameterSQLBindCol为 SQL_C_CHAR 或 SQL_C_BINARY。The C data type specified in SQLBindParameter or SQLBindCol is SQL_C_CHAR or SQL_C_BINARY.

  • C 数据类型为 SQL_C_DEFAULT,并且指定 SQL 数据类型的默认 C 数据类型为 SQL_C_CHAR 或 SQL_C_BINARY。The C data type is SQL_C_DEFAULT, and the default C data type for the specified SQL data type is SQL_C_CHAR or SQL_C_BINARY.

对于所有其他类型的 C 数据,如果StrLen_or_Ind不是 SQL_NULL_DATA 或 SQL_DEFAULT_PARAM,驱动程序假定的大小* DataPtr缓冲区是指定的 C 数据类型的大小与ValueTypeTargetType ,并将发送整个数据值。For all other types of C data, if StrLen_or_Ind is not SQL_NULL_DATA or SQL_DEFAULT_PARAM, the driver assumes that the size of the *DataPtr buffer is the size of the C data type specified with ValueType or TargetType and sends the entire data value. 有关详细信息,请参阅转换将数据从 C 到 SQL 数据类型中附录 d:数据类型。For more information, see Converting Data from C to SQL Data Types in Appendix D: Data Types.

返回Returns

SQL_SUCCESS、 SQL_SUCCESS_WITH_INFO、 SQL_STILL_EXECUTING、 SQL_ERROR 或 SQL_INVALID_HANDLE。SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_STILL_EXECUTING, SQL_ERROR, or SQL_INVALID_HANDLE.

诊断Diagnostics

SQLPutData返回 SQL_ERROR 或 SQL_SUCCESS_WITH_INFO,关联的 SQLSTATE 值可以通过调用来获取SQLGetDiagRecHandleType的 SQL_HANDLE_STMT 和一个处理StatementHandleWhen SQLPutData returns SQL_ERROR or SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value can be obtained by calling SQLGetDiagRec with a HandleType of SQL_HANDLE_STMT and a Handle of StatementHandle. 下表列出了通常返回的 SQLSTATE 值SQLPutData ,并解释了此函数; 每个上下文中的表示法"(数据挖掘)"之前 SQLSTATEs 返回由驱动程序管理器的说明。The following table lists the SQLSTATE values commonly returned by SQLPutData and explains each one in the context of this function; the notation "(DM)" precedes the descriptions of SQLSTATEs returned by the Driver Manager. 与每个 SQLSTATE 值关联的返回代码是 SQL_ERROR,除非另有说明。The return code associated with each SQLSTATE value is SQL_ERROR, unless noted otherwise.

SQLSTATESQLSTATE ErrorError 描述Description
0100001000 常规警告General warning 特定于驱动程序的信息性消息。Driver-specific informational message. (函数返回 SQL_SUCCESS_WITH_INFO。)(Function returns SQL_SUCCESS_WITH_INFO.)
0100401004 字符串数据,右截断String data, right truncated 字符串或二进制数据返回为输出参数时截断了非空白字符或非 NULL 的二进制数据。String or binary data returned for an output parameter resulted in the truncation of nonblank character or non-NULL binary data. 如果它是一个字符串值,它是右侧被截断。If it was a string value, it was right-truncated. (函数返回 SQL_SUCCESS_WITH_INFO。)(Function returns SQL_SUCCESS_WITH_INFO.)
0700607006 受限制的数据类型属性冲突Restricted data type attribute violation 标识的数据值ValueType中的参数SQLBindParameter无法为数据类型由标识转换绑定的参数为ParameterType中的参数SQLBindParameterThe data value identified by the ValueType argument in SQLBindParameter for the bound parameter could not be converted to the data type identified by the ParameterType argument in SQLBindParameter.
07S0107S01 默认参数的使用无效Invalid use of default parameter 参数值,设置SQLBindParameter、 是 SQL_DEFAULT_PARAM,并且相应参数没有默认值。A parameter value, set with SQLBindParameter, was SQL_DEFAULT_PARAM, and the corresponding parameter did not have a default value.
08S0108S01 通讯链接失败Communication link failure 该驱动程序和驱动程序已连接到数据源之间的通信链接失败之前函数已完成处理。The communication link between the driver and the data source to which the driver was connected failed before the function completed processing.
2200122001 字符串数据,右截断String data, right truncation 字符或二进制值的列分配导致非空白 (字符) 或非 null (二进制) 字符或字节数的截断。The assignment of a character or binary value to a column resulted in the truncation of nonblank (character) or non-null (binary) characters or bytes.

中的 SQL_NEED_LONG_DATA_LEN 信息类型SQLGetInfo为"Y",而且更多的数据已发送 (数据类型为 SQL_LONGVARCHAR、 SQL_LONGVARBINARY、 或的长整型数据源特定的数据类型) 的长参数不是指定了与StrLen_or_IndPtr中的参数SQLBindParameterThe SQL_NEED_LONG_DATA_LEN information type in SQLGetInfo was "Y", and more data was sent for a long parameter (the data type was SQL_LONGVARCHAR, SQL_LONGVARBINARY, or a long data source-specific data type) than was specified with the StrLen_or_IndPtr argument in SQLBindParameter.

中的 SQL_NEED_LONG_DATA_LEN 信息类型SQLGetInfo为"Y",而且比中指定了更多的数据已发送长列 (数据类型为 SQL_LONGVARCHAR、 SQL_LONGVARBINARY、 或的长整型数据源特定的数据类型)对应于已添加或使用更新的数据行中的列长度的缓冲区SQLBulkOperations或更新,它SQLSetPosThe SQL_NEED_LONG_DATA_LEN information type in SQLGetInfo was "Y", and more data was sent for a long column (the data type was SQL_LONGVARCHAR, SQL_LONGVARBINARY, or a long data source-specific data type) than was specified in the length buffer corresponding to a column in a row of data that was added or updated with SQLBulkOperations or updated with SQLSetPos.
2200322003 数值超出范围Numeric value out of range 数据将发送有关绑定的数值参数或列导致数字被截断时分配给关联的表的列的整个 (而不是小数) 部分。The data sent for a bound numeric parameter or column caused the whole (as opposed to fractional) part of the number to be truncated when assigned to the associated table column.

返回一个数字值 (数值或字符串) 的一个或多个输入/输出参数或输出参数将导致要截断的数字的整个 (而不是小数) 部分。Returning a numeric value (as numeric or string) for one or more input/output or output parameters would have caused the whole (as opposed to fractional) part of the number to be truncated.
2200722007 日期时间格式无效Invalid datetime format 发送参数或已绑定到日期、 时间或时间戳结构的列的数据时,分别,无效的日期、 时间戳。The data sent for a parameter or column that was bound to a date, time, or timestamp structure was, respectively, an invalid date, time, or timestamp.

输入/输出或输出参数绑定到日期、 时间或时间戳 C 结构,返回的参数中的值时,分别,无效的日期、 时间戳。An input/output or output parameter was bound to a date, time, or timestamp C structure, and a value in the returned parameter was, respectively, an invalid date, time, or timestamp. (函数返回 SQL_SUCCESS_WITH_INFO。)(Function returns SQL_SUCCESS_WITH_INFO.)
2200822008 日期时间字段溢出Datetime field overflow 日期时间表达式计算的输入/输出或输出参数导致了日期、 时间或时间戳 C 结构无效。A datetime expression computed for an input/output or output parameter resulted in a date, time, or timestamp C structure that was invalid.
2201222012 被零除Division by zero 算术表达式计算的输入/输出或输出参数导致除零。An arithmetic expression calculated for an input/output or output parameter resulted in division by zero.
2201522015 间隔字段溢出Interval field overflow 为确切的数值或时间间隔列发送数据或间隔 SQL 数据类型的参数时导致重要数字丢失。The data sent for an exact numeric or interval column or parameter to an interval SQL data type caused a loss of significant digits.

数据已发送间隔列或多个字段参数、 已转换为数值数据类型,和的数值数据类型中有没有表示形式。Data was sent for an interval column or parameter with more than one field, was converted to a numeric data type, and had no representation in the numeric data type.

列发送数据或参数数据分配到的时间间隔的 SQL 类型,并且没有在时间间隔内 SQL 类型的 C 类型的值没有表示形式。The data sent for column or parameter data was assigned to an interval SQL type, and there was no representation of the value of the C type in the interval SQL type.

精确数字或时间间隔 C 列或参数 C 间隔类型发送的数据时导致重要数字丢失。The data sent for an exact numeric or interval C column or parameter to an interval C type caused a loss of significant digits.

列发送数据或参数数据已分配给一个 C 间隔结构,没有间隔数据结构中的数据的表示形式。The data sent for column or parameter data was assigned to an interval C structure, and there was no representation of the data in the interval data structure.
2201822018 转换指定的字符值无效Invalid character value for cast specification C 类型为精确或近似数值、 日期时间或间隔数据类型;列的 SQL 类型是字符数据类型;和中的列或参数的值不是有效的绑定 C 类型的文本。The C type was an exact or approximate numeric, a datetime, or an interval data type; the SQL type of the column was a character data type; and the value in the column or parameter was not a valid literal of the bound C type.

SQL 类型是精确或近似数值、 日期时间或间隔数据类型;C 类型为 SQL_C_CHAR;和中的列或参数的值不是绑定的 SQL 类型的有效文本。The SQL type was an exact or approximate numeric, a datetime, or an interval data type; the C type was SQL_C_CHAR; and the value in the column or parameter was not a valid literal of the bound SQL type.
HY000HY000 常规错误General error 有关其中没有任何特定的 SQLSTATE 和为其定义任何特定于实现的 SQLSTATE 出错。An error occurred for which there was no specific SQLSTATE and for which no implementation-specific SQLSTATE was defined. 返回的错误消息SQLGetDiagRec*MessageText缓冲区描述错误以及其原因。The error message returned by SQLGetDiagRec in the *MessageText buffer describes the error and its cause.
HY001HY001 内存分配错误Memory allocation error 该驱动程序无法分配支持执行或完成该函数所需的内存。The driver was unable to allocate memory required to support execution or completion of the function.
HY008HY008 操作已取消Operation canceled 异步处理的已启用StatementHandleAsynchronous processing was enabled for the StatementHandle. 调用该函数,和之前执行完毕SQLCancelSQLCancelHandle上调用了StatementHandleThe function was called, and before it completed execution, SQLCancel or SQLCancelHandle was called on the StatementHandle. 然后在再次调用该函数StatementHandleThen the function was called again on the StatementHandle.

调用该函数,和之前执行完毕SQLCancelSQLCancelHandle上调用了StatementHandle来自不同线程中多线程应用程序。The function was called, and before it completed execution, SQLCancel or SQLCancelHandle was called on the StatementHandle from a different thread in a multithread application.
HY009HY009 使用空指针无效Invalid use of null pointer (DM) 参数DataPtr是空指针和参数StrLen_or_Ind不是 0,SQL_DEFAULT_PARAM 或 SQL_NULL_DATA。(DM) The argument DataPtr was a null pointer, and the argument StrLen_or_Ind was not 0, SQL_DEFAULT_PARAM, or SQL_NULL_DATA.
HY010HY010 函数序列错误Function sequence error (DM) 以前的函数调用不是调用SQLPutDataSQLParamData(DM) The previous function call was not a call to SQLPutData or SQLParamData.

(DM) 为与之关联的连接句柄调用以异步方式执行的函数StatementHandle(DM) An asynchronously executing function was called for the connection handle that is associated with the StatementHandle. 此异步函数仍在执行调用 SQLPutData 函数时。This asynchronous function was still executing when the SQLPutData function was called.

(数据挖掘) SQLExecuteSQLExecDirect,或SQLMoreResults曾为StatementHandle和返回 SQL_PARAM_DATA_可用。(DM) SQLExecute, SQLExecDirect, or SQLMoreResults was called for the StatementHandle and returned SQL_PARAM_DATA_AVAILABLE. 数据已检索到的所有经过流处理参数之前调用此函数。This function was called before data was retrieved for all streamed parameters.

(DM) 的调用以异步方式执行的函数 (不是此类似) StatementHandle和仍在执行时调用此函数。(DM) An asynchronously executing function (not this one) was called for the StatementHandle and was still executing when this function was called.
HY013HY013 内存管理错误Memory management error 无法处理函数调用,因为基础内存对象无法访问,可能是由于内存不足的情况。The function call could not be processed because the underlying memory objects could not be accessed, possibly because of low memory conditions.
HY019HY019 分段发送非字符和非二进制数据Non-character and non-binary data sent in pieces SQLPutData已被调用超过一次参数或列中,并且从未被使用它将字符 C 数据发送到包含的字符、 二进制或数据源特定的数据类型的列,或将二进制 C 数据发送到具有一个字符的列二进制文件或数据源特定的数据类型。SQLPutData was called more than once for a parameter or column, and it was not being used to send character C data to a column with a character, binary, or data source-specific data type or to send binary C data to a column with a character, binary, or data source-specific data type.
HY020HY020 尝试连接 null 值Attempt to concatenate a null value SQLPutData以来的调用返回 SQL_NEED_DATA,和一个这些调用中多次调用StrLen_or_Ind参数包含 SQL_NULL_DATA 或 SQL_DEFAULT_PARAM。SQLPutData was called more than once since the call that returned SQL_NEED_DATA, and in one of those calls, the StrLen_or_Ind argument contained SQL_NULL_DATA or SQL_DEFAULT_PARAM.
HY090HY090 字符串或缓冲区长度无效Invalid string or buffer length 自变量DataPtr不是 null 指针和参数StrLen_or_Ind小于 0,但不是等于 SQL_NTS 或 SQL_NULL_DATA。The argument DataPtr was not a null pointer, and the argument StrLen_or_Ind was less than 0 but not equal to SQL_NTS or SQL_NULL_DATA.
HY117HY117 由于未知的事务状态而挂起连接。Connection is suspended due to unknown transaction state. 仅断开连接,并允许使用只读的函数。Only disconnect and read-only functions are allowed. (DM) 有关挂起状态的详细信息,请参阅SQLEndTran 函数(DM) For more information about suspended state, see SQLEndTran Function.
HYT01HYT01 连接超时时间已到Connection timeout expired 连接超时期限过期之前的数据源响应此请求。The connection timeout period expired before the data source responded to the request. 通过设置连接超时期SQLSetConnectAttr,SQL_ATTR_CONNECTION_TIMEOUT。The connection timeout period is set through SQLSetConnectAttr, SQL_ATTR_CONNECTION_TIMEOUT.
IM001IM001 驱动程序不支持此函数Driver does not support this function (DM) 驱动程序与相关联StatementHandle不支持该函数。(DM) The driver associated with the StatementHandle does not support the function.
IM017IM017 轮询异步通知模式中禁用Polling is disabled in asynchronous notification mode 只要使用通知模型,将禁用轮询。Whenever the notification model is used, polling is disabled.
IM018IM018 SQLCompleteAsync尚未调用以完成此句柄上以前的异步操作。SQLCompleteAsync has not been called to complete the previous asynchronous operation on this handle. 如果句柄上的上一个函数调用返回 SQL_STILL_EXECUTING,如果启用通知模式,则SQLCompleteAsync必须要对其进行后期处理并完成该操作的句柄上调用。If the previous function call on the handle returns SQL_STILL_EXECUTING and if notification mode is enabled, SQLCompleteAsync must be called on the handle to do post-processing and complete the operation.

如果SQLPutData调用时发送参数数据中的 SQL 语句,它可以返回任何可由函数调用以执行该语句返回的 SQLSTATE (SQLExecuteSQLExecDirect)。If SQLPutData is called while sending data for a parameter in an SQL statement, it can return any SQLSTATE that can be returned by the function called to execute the statement (SQLExecute or SQLExecDirect). 如果发送数据的列时调用正在进行更新或添加具有SQLBulkOperations或使用更新SQLSetPos,它可以返回任何可由返回的 SQLSTATE SQLBulkOperationsSQLSetPosIf it is called while sending data for a column being updated or added with SQLBulkOperations or being updated with SQLSetPos, it can return any SQLSTATE that can be returned by SQLBulkOperations or SQLSetPos.

注释Comments

SQLPutData可以调用来提供这两种用法的数据执行时数据: 对的调用中使用的参数数据SQLExecuteSQLExecDirect,或更新行时使用的列数据或通过调用添加SQLBulkOperations或通过调用更新SQLSetPosSQLPutData can be called to supply data-at-execution data for two uses: parameter data to be used in a call to SQLExecute or SQLExecDirect, or column data to be used when a row is updated or added by a call to SQLBulkOperations or is updated by a call to SQLSetPos.

当应用程序调用SQLParamData来确定哪些数据应发送,该驱动程序返回的应用程序可用于确定要发送的参数数据或列数据所在的指示器。When an application calls SQLParamData to determine which data it should send, the driver returns an indicator that the application can use to determine which parameter data to send or where column data can be found. 它还会返回 SQL_NEED_DATA,这是一个指示符,则应调用的应用程序SQLPutData发送数据。It also returns SQL_NEED_DATA, which is an indicator to the application that it should call SQLPutData to send the data. 在中DataPtr自变量SQLPutData,应用程序将指针传递到包含实际数据的参数或列的缓冲区。In the DataPtr argument to SQLPutData, the application passes a pointer to the buffer containing the actual data for the parameter or column.

当驱动程序返回 SQL_SUCCESS 有关SQLPutData,应用程序调用SQLParamData试。When the driver returns SQL_SUCCESS for SQLPutData, the application calls SQLParamData again. SQLParamData返回 sql_need_data,这更多数据需要发送,如果在此情况下应用程序调用SQLPutData试。SQLParamData returns SQL_NEED_DATA if more data needs to be sent, in which case the application calls SQLPutData again. 如果已发送所有数据执行时数据,它将返回 SQL_SUCCESS。It returns SQL_SUCCESS if all data-at-execution data has been sent. 然后,应用程序调用SQLParamData试。The application then calls SQLParamData again. 如果驱动程序将返回 SQL_NEED_DATA 和中的另一个指示器 *ValuePtrPtr,它需要另一个参数或列的数据并SQLPutData再次调用。If the driver returns SQL_NEED_DATA and another indicator in *ValuePtrPtr, it requires data for another parameter or column and SQLPutData is called again. 如果驱动程序将返回 SQL_SUCCESS,然后所有数据在执行发送数据,并可以执行 SQL 语句或SQLBulkOperationsSQLSetPos调用可以进行处理。If the driver returns SQL_SUCCESS, then all data-at-execution data has been sent and the SQL statement can be executed or the SQLBulkOperations or SQLSetPos call can be processed.

在语句执行时传递的数据在执行参数数据的详细信息,请参阅"传递参数值"中SQLBindParameter发送长数据For more information on how data-at-execution parameter data is passed at statement execution time, see "Passing Parameter Values" in SQLBindParameter and Sending Long Data. 有关如何执行时的数据列数据的详细信息更新或添加,请参阅"使用 SQLSetPos"一节中SQLSetPos,"执行大容量更新使用中的书签" SQLBulkOperations,和Long 数据和 SQLSetPos 及 SQLBulkOperationsFor more information on how data-at-execution column data is updated or added, see the section "Using SQLSetPos" in SQLSetPos, "Performing Bulk Updates Using Bookmarks" in SQLBulkOperations, and Long Data and SQLSetPos and SQLBulkOperations.

备注

应用程序可以使用SQLPutData发送部分仅当将字符 C 数据发送到特定于源的数据类型为字符、 二进制文件或数据列,或将二进制 C 数据发送到具有二进制文件,一个字符的列中的数据特定于源的数据类型。An application can use SQLPutData to send data in parts only when sending character C data to a column with a character, binary, or data source-specific data type or when sending binary C data to a column with a character, binary, or data source-specific data type. 如果SQLPutData调用一次以上任何其他情况下,它将返回 SQL_ERROR 和 SQLSTATE hy019 分段 (分段发送非字符和非二进制数据)。If SQLPutData is called more than once under any other conditions, it returns SQL_ERROR and SQLSTATE HY019 (Non-character and non-binary data sent in pieces).

示例Example

下面的示例假定名为 Test 的数据源名称。The following sample assumes a data source name called Test. 关联的数据库应具有您可以创建,按如下所示的表:The associated database should have a table that you can create, as follows:

CREATE TABLE emp4 (NAME char(30), AGE int, BIRTHDAY datetime, Memo1 text)  
// SQLPutData.cpp  
// compile with: odbc32.lib user32.lib  
#include <stdio.h>  
#include <windows.h>  
#include <sqlext.h>  
#include <odbcss.h>  
  
#define TEXTSIZE  12000  
#define MAXBUFLEN 256  
  
SQLHENV henv = SQL_NULL_HENV;  
SQLHDBC hdbc1 = SQL_NULL_HDBC;       
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;  
  
void Cleanup() {  
   if (hstmt1 != SQL_NULL_HSTMT)  
      SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);  
  
   if (hdbc1 != SQL_NULL_HDBC) {  
      SQLDisconnect(hdbc1);  
      SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);  
   }  
  
   if (henv != SQL_NULL_HENV)  
      SQLFreeHandle(SQL_HANDLE_ENV, henv);  
}  
  
int main() {  
   RETCODE retcode;  
  
   // SQLBindParameter variables.  
   SQLLEN cbTextSize, lbytes;  
  
   // SQLParamData variable.  
   PTR pParmID;  
  
   // SQLPutData variables.  
   UCHAR  Data[] =   
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"  
      "abcdefghijklmnopqrstuvwxyz";  
  
   SDWORD cbBatch = (SDWORD)sizeof(Data) - 1;  
  
   // Allocate the ODBC environment and save handle.  
   retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);  
   if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {  
      printf("SQLAllocHandle(Env) Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Notify ODBC that this is an ODBC 3.0 app.  
   retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);  
   if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {  
      printf("SQLSetEnvAttr(ODBC version) Failed\n\n");  
      Cleanup();  
      return(9);      
   }  
  
   // Allocate ODBC connection handle and connect.  
   retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);  
   if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {  
      printf("SQLAllocHandle(hdbc1) Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Sample uses Integrated Security, create SQL Server DSN using Windows NT authentication.   
   retcode = SQLConnect(hdbc1, (UCHAR*)"Test", SQL_NTS, (UCHAR*)"",SQL_NTS, (UCHAR*)"", SQL_NTS);  
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {  
      printf("SQLConnect() Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Allocate statement handle.  
   retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);  
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {  
      printf("SQLAllocHandle(hstmt1) Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Set parameters based on total data to send.  
   lbytes = (SDWORD)TEXTSIZE;  
   cbTextSize = SQL_LEN_DATA_AT_EXEC(lbytes);  
  
   // Bind the parameter marker.  
   retcode = SQLBindParameter (hstmt1,           // hstmt  
                               1,                // ipar  
                               SQL_PARAM_INPUT,  // fParamType  
                               SQL_C_CHAR,       // fCType  
                               SQL_LONGVARCHAR,  // FSqlType  
                               lbytes,           // cbColDef  
                               0,                // ibScale  
                               (VOID *)1,        // rgbValue  
                               0,                // cbValueMax  
                               &cbTextSize);     // pcbValue  
  
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {  
      printf("SQLBindParameter Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Execute the command.  
   retcode =   
      SQLExecDirect(hstmt1, (UCHAR*)"INSERT INTO emp4 VALUES('Paul Borm', 46,'1950-11-12 00:00:00', ?)", SQL_NTS);  
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_NEED_DATA) && (retcode != SQL_SUCCESS_WITH_INFO) ) {  
      printf("SQLExecDirect Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Check to see if NEED_DATA; if yes, use SQLPutData.  
   retcode = SQLParamData(hstmt1, &pParmID);  
   if (retcode == SQL_NEED_DATA) {  
      while (lbytes > cbBatch) {  
         SQLPutData(hstmt1, Data, cbBatch);  
         lbytes -= cbBatch;  
      }  
      // Put final batch.  
      retcode = SQLPutData(hstmt1, Data, lbytes);   
   }  
  
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {  
      printf("SQLParamData Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Make final SQLParamData call.  
   retcode = SQLParamData(hstmt1, &pParmID);  
   if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {  
      printf("Final SQLParamData Failed\n\n");  
      Cleanup();  
      return(9);  
   }  
  
   // Clean up.  
   SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);  
   SQLDisconnect(hdbc1);  
   SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);  
   SQLFreeHandle(SQL_HANDLE_ENV, henv);  
}  
有关信息For information about 请参阅See
绑定到参数的缓冲区Binding a buffer to a parameter SQLBindParameter 函数SQLBindParameter Function
取消语句处理Canceling statement processing SQLCancel 函数SQLCancel Function
执行 SQL 语句Executing an SQL statement SQLExecDirect 函数SQLExecDirect Function
执行已准备的 SQL 语句Executing a prepared SQL statement SQLExecute 函数SQLExecute Function
返回下一个参数将数据发送用于Returning the next parameter to send data for SQLParamData 函数SQLParamData Function

请参阅See Also

ODBC API 参考 ODBC API Reference
ODBC 头文件ODBC Header Files