我正在使用 odbc 在 native c++ 中创建应用程序。我正在调用具有小数参数的 sql server 存储过程。我正在将一个 float 传递给该参数。到目前为止的代码:
SQLDECIMAL *sql_param = new SQLDECIMAL( param);
if( SQL_ERROR == ( result = SQLBindParameter( statement_handle, 5, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_DECIMAL, 12, 6, sql_param, 0, NULL ) ) ){
std::wstring error = get_error_message( SQL_HANDLE_STMT, statement_handle );
throw GenericException( error );
}
参数是一个 float 。
存储在表中的值始终为 0,而不是实际值 1.45。我猜正在进行一些转换,但我无法找出正确的转换。 SQL_C_FLOAT -> SQL_DECIMAL?
最佳答案
在reference :
SQLRETURN SQLBindParameter(
SQLHSTMT StatementHandle,
SQLUSMALLINT ParameterNumber,
SQLSMALLINT InputOutputType,
SQLSMALLINT ValueType,
SQLSMALLINT ParameterType,
SQLULEN ColumnSize,
SQLSMALLINT DecimalDigits,
SQLPOINTER ParameterValuePtr,
SQLLEN BufferLength,
SQLLEN * StrLen_or_IndPtr);
...
BufferLength
[Input/Output] Length of the ParameterValuePtr buffer in bytes.
您已将 BufferLength
指定为零。寻找 here ,你可以看到不同精度值的小数长度:
Precision StorageBytes
1-9 5
10-19 9
20-28 13
29-38 17
因此,您需要将方法SQLBindParameter
调用为:
SQLBindParameter( statement_handle, 1, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_DECIMAL, 12, 6, sql_param, 5, NULL )
关于c++ float to sql server DECIMAL正确转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18285151/