SQLBindParameter使用C ++和SQL Native Client准备SQLPutData

时间:2022-09-01 23:26:22

I'm trying to use SQLBindParameter to prepare my driver for input via SQLPutData. The field in the database is a TEXT field. My function is crafted based on MS's example here: http://msdn.microsoft.com/en-us/library/ms713824(VS.85).aspx.

我正在尝试使用SQLBindParameter来准备我的驱动程序以通过SQLPutData输入。数据库中的字段是TEXT字段。我的功能基于MS的示例精心制作:http://msdn.microsoft.com/en-us/library/ms713824(VS.85).aspx。

I've setup the environment, made the connection, and prepared my statement successfully but when I call SQLBindParam (using code below) it consistently fails reporting: [Microsoft][SQL Native Client]Invalid precision value

我已经设置了环境,建立了连接,并成功准备了我的语句但是当我调用SQLBindParam时(使用下面的代码)它始终无法报告:[Microsoft] [SQL Native Client]无效的精度值

int col_num = 1;
SQLINTEGER length = very_long_string.length( );
retcode = SQLBindParameter( StatementHandle,
            col_num,
            SQL_PARAM_INPUT,
            SQL_C_BINARY,
            SQL_LONGVARBINARY,
            NULL,
            NULL,            
            (SQLPOINTER) col_num,     
            NULL,                 
            &length ); 

The above relies on the driver in use returning "N" for the SQL_NEED_LONG_DATA_LEN information type in SQLGetInfo. My driver returns "Y". How do I bind so that I can use SQLPutData?

以上依赖于正在使用的驱动程序,在SQLGetInfo中为SQL_NEED_LONG_DATA_LEN信息类型返回“N”。我的司机返回“Y”。如何绑定以便我可以使用SQLPutData?

2 个解决方案

#1


1  

you're passing NULL as the buffer length, this is an in/out param that shoudl be the size of the col_num parameter. Also, you should pass a value for the ColumnSize or DecimalDigits parameters.

你传递NULL作为缓冲区长度,这是一个输入/输出参数,它应该是col_num参数的大小。此外,您应该传递ColumnSize或DecimalDigits参数的值。

http://msdn.microsoft.com/en-us/library/ms710963(VS.85).aspx

#2


3  

Though it doesn't look just like the documentation's example code, I found the following solution to work for what I'm trying to accomplish. Thanks gbjbaanb for making me retest my input combinations to SQLBindParameter.

虽然它看起来不像文档的示例代码,但我发现以下解决方案适用于我正在尝试完成的任务。感谢gbjbaanb让我重新测试我的输入组合到SQLBindParameter。

    SQLINTEGER length;
    RETCODE retcode = SQLBindParameter( StatementHandle,
        col_num,      // position of the parameter in the query
        SQL_PARAM_INPUT,
        SQL_C_CHAR,
        SQL_VARCHAR,
        data_length,        // size of our data
        NULL,               // decimal precision: not used our data types
        &my_string,         // SQLParamData will return this value later to indicate what data it's looking for so let's pass in the address of our std::string
        data_length,
        &length );          // it needs a length buffer

    // length in the following operation must still exist when SQLExecDirect or SQLExecute is called
    // in my code, I used a pointer on the heap for this.
    length = SQL_LEN_DATA_AT_EXEC( data_length ); 

After a statement is executed, you can use SQLParamData to determine what data SQL wants you to send it as follows:

执行语句后,您可以使用SQLParamData来确定SQL要将您发送的数据,如下所示:

    std::string* my_string;
    // set string pointer to value given to SQLBindParameter
    retcode = SQLParamData( StatementHandle, (SQLPOINTER*) &my_string ); 

Finally, use SQLPutData to send the contents of your string to SQL:

最后,使用SQLPutData将字符串的内容发送到SQL:

    // send data in chunks until everything is sent
    SQLINTEGER len;
    for ( int i(0); i < my_string->length( ); i += CHUNK_SIZE )
    {
        std::string substr = my_string->substr( i, CHUNK_SIZE );

        len = substr.length( );

        retcode = SQLPutData( StatementHandle, (SQLPOINTER) substr.c_str( ), len );
    }

#1


1  

you're passing NULL as the buffer length, this is an in/out param that shoudl be the size of the col_num parameter. Also, you should pass a value for the ColumnSize or DecimalDigits parameters.

你传递NULL作为缓冲区长度,这是一个输入/输出参数,它应该是col_num参数的大小。此外,您应该传递ColumnSize或DecimalDigits参数的值。

http://msdn.microsoft.com/en-us/library/ms710963(VS.85).aspx

#2


3  

Though it doesn't look just like the documentation's example code, I found the following solution to work for what I'm trying to accomplish. Thanks gbjbaanb for making me retest my input combinations to SQLBindParameter.

虽然它看起来不像文档的示例代码,但我发现以下解决方案适用于我正在尝试完成的任务。感谢gbjbaanb让我重新测试我的输入组合到SQLBindParameter。

    SQLINTEGER length;
    RETCODE retcode = SQLBindParameter( StatementHandle,
        col_num,      // position of the parameter in the query
        SQL_PARAM_INPUT,
        SQL_C_CHAR,
        SQL_VARCHAR,
        data_length,        // size of our data
        NULL,               // decimal precision: not used our data types
        &my_string,         // SQLParamData will return this value later to indicate what data it's looking for so let's pass in the address of our std::string
        data_length,
        &length );          // it needs a length buffer

    // length in the following operation must still exist when SQLExecDirect or SQLExecute is called
    // in my code, I used a pointer on the heap for this.
    length = SQL_LEN_DATA_AT_EXEC( data_length ); 

After a statement is executed, you can use SQLParamData to determine what data SQL wants you to send it as follows:

执行语句后,您可以使用SQLParamData来确定SQL要将您发送的数据,如下所示:

    std::string* my_string;
    // set string pointer to value given to SQLBindParameter
    retcode = SQLParamData( StatementHandle, (SQLPOINTER*) &my_string ); 

Finally, use SQLPutData to send the contents of your string to SQL:

最后,使用SQLPutData将字符串的内容发送到SQL:

    // send data in chunks until everything is sent
    SQLINTEGER len;
    for ( int i(0); i < my_string->length( ); i += CHUNK_SIZE )
    {
        std::string substr = my_string->substr( i, CHUNK_SIZE );

        len = substr.length( );

        retcode = SQLPutData( StatementHandle, (SQLPOINTER) substr.c_str( ), len );
    }