C++ SQLBindParameter,用于使用C++;和SQL本机客户端
我正试图使用C++ SQLBindParameter,用于使用C++;和SQL本机客户端,c++,sql,sql-server,sqlncli,C++,Sql,Sql Server,Sqlncli,我正试图使用sqlbindparmeter来准备我的驱动程序,以便通过SQLPutData进行输入。数据库中的字段是文本字段。我的函数是根据MS的示例精心编制的: 我已经设置了环境,建立了连接,并成功地准备了我的语句,但是当我调用SQLBindParam(使用下面的代码)时,它始终无法报告:[Microsoft][SQL Native Client]精度值无效 int col_num = 1; SQLINTEGER length = very_long_string.length( ); re
sqlbindparmeter
来准备我的驱动程序,以便通过SQLPutData
进行输入。数据库中的字段是文本
字段。我的函数是根据MS的示例精心编制的:
我已经设置了环境,建立了连接,并成功地准备了我的语句,但是当我调用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 );
上述情况取决于正在使用的驱动程序在
SQLGetInfo
中为SQL\u NEED\u LONG\u DATA\u LEN
信息类型返回“N”。我的司机返回“Y”。如何绑定以便使用SQLPutData
?如果要传递NULL作为缓冲区长度,这是一个in/out参数,应该是col_num参数的大小。此外,还应传递ColumnSize或DecimalDigits参数的值
您要传递NULL作为缓冲区长度,这是一个in/out参数,应该是col_num参数的大小。此外,还应传递ColumnSize或DecimalDigits参数的值
尽管它看起来不像文档的示例代码,但我发现以下解决方案适用于我试图实现的目标。感谢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 );
执行语句后,您可以使用SQLParamData确定SQL希望您发送的数据,如下所示:
std::string* my_string;
// set string pointer to value given to SQLBindParameter
retcode = SQLParamData( StatementHandle, (SQLPOINTER*) &my_string );
最后,使用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 );
}
//将数据分块发送,直到发送完所有内容
SQLINTEGER-len;
for(inti(0);ilength();i+=CHUNK\u SIZE)
{
std::string substr=my\u string->substr(i,块大小);
len=子字符串长度();
retcode=SQLPutData(StatementHandle,(SQLPOINTER)substr.c_str(),len);
}
虽然它看起来不像文档中的示例代码,但我发现以下解决方案适用于我试图实现的目标。感谢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 );
执行语句后,您可以使用SQLParamData确定SQL希望您发送的数据,如下所示:
std::string* my_string;
// set string pointer to value given to SQLBindParameter
retcode = SQLParamData( StatementHandle, (SQLPOINTER*) &my_string );
最后,使用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 );
}
//将数据分块发送,直到发送完所有内容
SQLINTEGER-len;
for(inti(0);ilength();i+=CHUNK\u SIZE)
{
std::string substr=my\u string->substr(i,块大小);
len=子字符串长度();
retcode=SQLPutData(StatementHandle,(SQLPOINTER)substr.c_str(),len);
}
MSDN示例每次都中断,我仍然没有让我的工作:(–MSDN示例每次中断,我仍然没有让我的工作:(–+1注释“调用SQLExecDirect或SQLExecute时,以下操作中的长度必须仍然存在”。由于ODBC似乎通过同时发送Bind和Execute调用的结果来进行优化,因此我在此过程中遇到了一些范围错误。+1注释“调用SQLExecDirect或SQLExecute时,以下操作中的长度必须仍然存在”.由于ODBC似乎通过同时发送Bind和Execute调用的结果来进行优化,因此我在此过程中遇到了一些范围错误。