首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C++和SQL Native Client为SQLPutData做准备的SQLBindParameter

使用C++和SQL Native Client为SQLPutData做准备的SQLBindParameter
EN

Stack Overflow用户
提问于 2008-09-17 14:51:34
回答 2查看 4.9K关注 0票数 1

我正在尝试使用SQLBindParameter为通过SQLPutData输入的驱动程序做准备。数据库中的字段是TEXT字段。我的函数是根据MS的例子制作的:http://msdn.microsoft.com/en-us/library/ms713824(VS.85).aspx

我已经成功地设置了环境、建立了连接并准备好了我的语句,但是当我调用SQLBindParam (使用下面的代码)时,它总是无法报告:[Microsoft][SQL Native Client]Invalid precision value

代码语言:javascript
复制
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 ); 

以上依赖于使用中的驱动程序为SQLGetInfo中的SQL_NEED_LONG_DATA_LEN信息类型返回"N“。我的驱动程序返回"Y“。如何绑定才能使用SQLPutData

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-09-17 16:35:18

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

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

票数 1
EN

Stack Overflow用户

发布于 2008-09-18 02:17:40

尽管它看起来不像文档的示例代码,但我找到了以下解决方案来实现我想要实现的目标。感谢gbjbaanb让我重新测试SQLBindParameter的输入组合。

代码语言:javascript
复制
    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 ); 

语句执行后,您可以使用SQLParamData来确定SQL希望您发送什么数据,如下所示:

代码语言:javascript
复制
    std::string* my_string;
    // set string pointer to value given to SQLBindParameter
    retcode = SQLParamData( StatementHandle, (SQLPOINTER*) &my_string ); 

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

代码语言:javascript
复制
    // 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 );
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/84064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档