如何从SQL Server中读取数字/小数/金额列? [英] How to read numeric/decimal/money columns from SQL Server?

查看:223
本文介绍了如何从SQL Server中读取数字/小数/金额列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以以下方式通过ODBC从SQL Server中读取数字/小数/货币列:

I try to read numeric/decimal/money columns from SQL Server via ODBC in the following manner:

SQL_NUMERIC_STRUCT decimal;
SQLGetData(hSqlStmt, iCol, SQL_C_NUMERIC, &decimal, sizeof(decimal), &indicator);

所有这些类型都以SQL_NUMERIC_STRUCT结构形式返回,我将SQL_C_NUMERIC类型指定为SQLGetData()API.

All these types are returned as SQL_NUMERIC_STRUCT structure, and I specify the SQL_C_NUMERIC type to SQLGetData() API.

在数据库中,列定义为例如decimal(18, 4)money.但是问题在于返回的数据始终将decimal.precision设置为38(最大可能值),而decimal.scale始终设置为零.因此,如果实际存储的数字为 12.65 ,则SQL_NUMERIC_STRUCT结构中的返回值等于 12 .因此,由于一个非常奇怪的原因,小数部分被简单地丢弃了.

In the database, column is defined as decimal(18, 4) for example, or money. But the problem is that the returned data has decimal.precision always set to 38 (max possible value) and decimal.scale always set to zero. So if the actual stored number is 12.65, the returned value in SQL_NUMERIC_STRUCT structure is equal to 12. So the fractional part is simply discarded, for a very strange reason.

我该怎么办?

推荐答案

好,本文解释问题.该解决方案非常麻烦,以至于在受

OK, this article explains the problem. The solution is so cumbersome, that I decided to avoid using SQL_NUMERIC_STRUCT altogether, influenced by this post. Now I specify SQL_C_WCHAR (SQL_C_CHAR would do as well) and read the numeric/decimal/money columns as text strings directly. Looks like the driver does the conversion.

这篇关于如何从SQL Server中读取数字/小数/金额列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆