如何判断数据库数据类型 ad0

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 10:45   21   0

从数据库读数据到CELL时,用D方法只能读入数值型,用S方法只能读入字符型。请问如何判断字段的数据类型(比如:数值、文本、货币、逻辑型等…… ),谢谢。

发贴心情

在ADO中可以取得字段的类型,Fields(i).Type

COleVariant类

COleVariant 类通逢装了 VARIANT 结构。实际的数据就在 VARIANT 结构中。
使用 COleVariant 的两个操作
operator LPCVARIANT
Converts a COleVariant value into an LPCVARIANT.
operator LPVARIANT
Converts a COleVariant object into an LPVARIANT.
可以得到 VARIANT 结构。其区别是,前者是个拷贝操作,将 VARIANT 结构的内容拷贝到目标中去,后者仅仅返回
VARIANT 结构的指针。不管那种情况,现在我们都能访问到 VARIANT 所包含的数据了。余下的问题是如何解释数
据内容。
VARIANT 结构包含两部分。其一是 VARTYPE 型的成员变量vt;其二是个联合类型,这个联合包含了VC常用的几乎所有类型。因为联合用的是相同的存储空间,因此对联合的内容的解释依赖于 vt。
例如,
若 vt 的值是 VT_UI2, 说明该联合被解释为short int. 并使用成员变量名 iVal。
若 vt 的的值是 VT_BSTR,说明该联合被解释为 BSTR 类型。并使用成员变量名 bstrVal。
若 vt 的的值是 VT_BSTR|VT_BYREF,说明该联合被解释为BSTR 型指针。并使用成员变量名 pbstrVal。
(其他详见 VARIANT 的联机帮助)
*********************************
若从数据库返回的是简单类型,如 short, long, 等,则直接引用既可。(主持人注:COleVariant类重载了“=”操作符,所以常用类型可以直接转换)若返回的是字符串类型,则有可能是 bstrVal 或pbstrVal。依赖于数据库服务程序。 BSTR 实际上就是个unicode 字符串,CString 的构造函数和赋值操作都能直接识别这一类型。

Type Property

Indicates the operational type or data type of a Parameter, Field, or Property object.

Settings and Return Values

Sets or returns a DataTypeEnum value.

Remarks

For Parameter objects, the Type property is read/write. For new Field objects that have been appended to the Fields collection of a Record, Type is read/write only after the Value property for the Field has been specified and the data provider has successfully added the new Field by calling the Update method of the Fields collection.

For all other objects, the Type property is read-only.

DataTypeEnum

Specifies the data type of a Field, Parameter, or Property. The corresponding OLE DB type indicator is shown in parentheses in the description column of the following table. For more information about OLE DB data types, see Chapter 13: Data Types in OLE DB and Appendix A: Data Types of the OLE DB Programmer's Reference.

Constant Value Description
AdArray
(Does not apply to ADOX.)
0x2000 A flag value, always combined with another data type constant, that indicates an array of that other data type.
adBigInt 20 Indicates an eight-byte signed integer (DBTYPE_I8).
adBinary 128 Indicates a binary value (DBTYPE_BYTES).
adBoolean 11 Indicates a boolean value (DBTYPE_BOOL).
adBSTR 8 Indicates a null-terminated character string (Unicode) (DBTYPE_BSTR).
adChapter 136 Indicates a four-byte chapter value that identifies rows in a child rowset (DBTYPE_HCHAPTER).
adChar 129 Indicates a string value (DBTYPE_STR).
adCurrency 6 Indicates a currency value (DBTYPE_CY). Currency is a fixed-point number with four digits to the right of the decimal point. It is stored in an eight-byte signed integer scaled by 10,000.
adDate 7 Indicates a date value (DBTYPE_DATE). A date is stored as a double, the whole part of which is the number of days since December 30, 1899, and the fractional part of which is the fraction of a day.
adDBDate 133 Indicates a date value (yyyymmdd) (DBTYPE_DBDATE).
adDBTime 134 Indicates a time value (hhmmss) (DBTYPE_DBTIME).
adDBTimeStamp 135 Indicates a date/time stamp (yyyymmddhhmmss plus a fraction in billionths) (DBTYPE_DBTIMESTAMP).
adDecimal 14 Indicates an exact numeric value with a fixed precision and scale (DBTYPE_DECIMAL).
adDouble 5 Indicates a double-precision floating-point value (DBTYPE_R8).
adEmpty 0 Specifies no value (DBTYPE_EMPTY).
adError 10 Indicates a 32-bit error code (DBTYPE_ERROR).
adFileTime 64 Indicates a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (DBTYPE_FILETIME).
adGUID 72 Indicates a globally unique identifier (GUID) (DBTYPE_GUID).
adIDispatch 9 Indicates a pointer to an IDispatch interface on a COM object (DBTYPE_IDISPATCH).

Note This data type is currently not supported by ADO. Usage may cause unpredictable results.

adInteger 3 Indicates a four-byte signed integer (DBTYPE_I4).
adIUnknown 13 Indicates a pointer to an IUnknown interface on a COM object (DBTYPE_IUNKNOWN).

Note This data type is currently not supported by ADO. Usage may cause unpredictable results.

adLongVarBinary 205 Indicates a long binary value.
adLongVarChar 201 Indicates a long string value.
adLongVarWChar 203 Indicates a long null-terminated Unicode string value.
adNumeric 131 Indicates an exact numeric value with a fixed precision and scale (DBTYPE_NUMERIC).
adPropVariant 138 Indicates an Automation PROPVARIANT (DBTYPE_PROP_VARIANT).
adSingle 4 Indicates a single-precision floating-point value (DBTYPE_R4).
adSmallInt 2 Indicates a two-byte signed integer (DBTYPE_I2).
adTinyInt 16 Indicates a one-byte signed integer (DBTYPE_I1).
adUnsignedBigInt 21 Indicates an eight-byte unsigned integer (DBTYPE_UI8).
adUnsignedInt 19 Indicates a four-byte unsigned integer (DBTYPE_UI4).
adUnsignedSmallInt 18 Indicates a two-byte unsigned integer (DBTYPE_UI2).
adUnsignedTinyInt 17 Indicates a one-byte unsigned integer (DBTYPE_UI1).
adUserDefined 132 Indicates a user-defined variable (DBTYPE_UDT).
adVarBinary 204 Indicates a binary value.
adVarChar 200 Indicates a string value.
adVariant 12 Indicates an Automation Variant (DBTYPE_VARIANT).

Note This data type is currently not supported by ADO. Usage may cause unpredictable results.

adVarNumeric 139 Indicates a numeric value.
adVarWChar 202 Indicates a null-terminated Unicode character string.
adWChar 130 Indicates a null-terminated Unicode character string (DBTYPE_WSTR).

ADO/WFC Equivalent

Package: com.ms.wfc.data

Constant
AdoEnums.DataType.ARRAY
AdoEnums.DataType.BIGINT
AdoEnums.DataType.BINARY
AdoEnums.DataType.BOOLEAN
AdoEnums.DataType.BSTR
AdoEnums.DataType.CHAPTER
AdoEnums.DataType.CHAR
AdoEnums.DataType.CURRENCY
AdoEnums.DataType.DATE
AdoEnums.DataType.DBDATE
AdoEnums.DataType.DBTIME
AdoEnums.DataType.DBTIMESTAMP
AdoEnums.DataType.DECIMAL
AdoEnums.DataType.DOUBLE
AdoEnums.DataType.EMPTY
AdoEnums.DataType.ERROR
AdoEnums.DataType.FILETIME
AdoEnums.DataType.GUID
AdoEnums.DataType.IDISPATCH
AdoEnums.DataType.INTEGER
AdoEnums.DataType.IUNKNOWN
AdoEnums.DataType.LONGVARBINARY
AdoEnums.DataType.LONGVARCHAR
AdoEnums.DataType.LONGVARWCHAR
AdoEnums.DataType.NUMERIC
AdoEnums.DataType.PROPVARIANT
AdoEnums.DataType.SINGLE
AdoEnums.DataType.SMALLINT
AdoEnums.DataType.TINYINT
AdoEnums.DataType.UNSIGNEDBIGINT
AdoEnums.DataType.UNSIGNEDINT
AdoEnums.DataType.UNSIGNEDSMALLINT
AdoEnums.DataType.UNSIGNEDTINYINT
AdoEnums.DataType.USERDEFINED
AdoEnums.DataType.VARBINARY
AdoEnums.DataType.VARCHAR
AdoEnums.DataType.VARIANT
AdoEnums.DataType.VARNUMERIC
AdoEnums.DataType.VARWCHAR
AdoEnums.DataType.WCHAR

See Also

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP