今天心情不好,大家来讨论讨论DB_NUMERIC的用法吧
以前用OLEDB CONSUMER 的时候我一般都是在数据库中使用INT 和 CHAR,映射为C++的数据类型就是 LONG 和 TCHAR 。这些都是我熟悉的数据类型,所以一直没有什么大问题。这次毕业设计,因为是合作开发,在设计的时候考虑到数据表示范围,所以使用了NUMERIC和DECIMAL作为数据库字段类型。没想到烦恼就从这两个数据类型开始了。
追求时髦的缘故吧,我使用VC7.0,因为对VC6.0的熟悉,所以架构设计完成之后我就非常有信心的开始了代码的编写(我负责通过网络采集数据的一个模块)。一切都进展得很顺利,数据非常顺利的采集过来,并且被我解析成对象、以及对象的集合。正当我准备开始编写“数据入库”代码的时候,我才发现VC7.0和VC6.0中的ATL OLEDB CONSUMER大有不同的地方——原来是ATL的版本升级了,没有办法,我只好扎在MSDN中重新学习了一下VC7.0中的ATL的使用方法,写了点学习代码之后我才突然发现,我数据库中使用的NUMERIC和DECIMAL所映射的DB_NUMERIC我从来都没有使用过!查遍了MSDN也只找到DB_NUMERIC的定义tagDB_NUMERIC还有一个莫名其妙的解释
typedef struct tagNUMERIC {
BYTE precision;
BYTE scale;
BYTE sign;
BYTE val[16];
} DB_NUMERIC;
Members
precision
The maximum number of digits in base 10.
scale
The number of digits to the right of the decimal point.
sign
The sign is 1 for positive numbers, and 0 for negative numbers.
val
A number stored as a 16-byte scaled integer, with the least-significant byte on the left.
For example, to specify the base 10 number 20.003 with a scale of 4, the number is scaled to an integer of 200030 (20.003 shifted by four tens digits), which is 186AA in hexadecimal. The value stored in the 16-byte integer is 5E 0D 03 00 00 00 00 00 00 00 00 00 00 00 00 00, the precision is the maximum precision, the scale is 4, and the sign is 1.
大家看好了,从For example这一段仔细看 看到 “which is 186AA..." 我怎么也想不通:200030的16进制不是030d5e吗?(文章中 the value stored in the 16-byte.. 也说明了)怎么会是 186AA (100010 in decimal)呢?
还有就是decimal这么麻烦大家有没有什么好办法?一定要让我自己写类型转换函数吗?查了这儿的历史,去年12月有人问了这个问题,但是没有人回答,希望这次有人能回答了,小弟我谢谢大家了——因为心情实在不好,不想一个人钻牛角尖了。