Reference articles
Http://blog.163.com/[email protected]/blog/static/16219623020107634935586/
http://blog.csdn.net/dreamcs/article/details/7186633
Http://blog.sina.com.cn/s/blog_6c3d32da0100ua13.html
COleVariant is the data type that the database is used to. It can be a string, an integer value, a date, and so on. It's useful to know how to convert it to CString.
With CString A; COleVariant B; Let's see how to convert COleVariant to CString:
Switch (B.VT) {
Case VT_BSTR:A=V_BSTRT (&B); Break;//colevariant is a string
Case Vt_i2:a.format (_t ("%hd"), V_i2 (&b)); break;//is a short-integer
Case Vt_i4:a.format (_t ("%d"), V_I4 (&b)); break;//is a long integer
Case Vt_r4:a.format (_t ("%e"), (double) V_R4 (&b)); break;//is a floating-point number
Case Vt_r8:a.format (_t ("%e"), V_R8 (&b)); break;//is a floating-point number
Case Vt_cy:a=colecurrency (B). Format (); break;//is a currency value
Case Vt_date:a=coledatetime (B). Format ("%y-%m-%d"); break;//is a date
Case Vt_bool:a=v_bool (&b)? " True ":" False "; break;//is a Boolean value
}
//----------------------------------------------------------------------------------------------------------
I found A=V_BSTRT (&b); Only one character can be passed, there is a problem.
In the following way, two sentences can be solved directly. Haha, sometimes online things still have to think about
B.changetype (VT_BSTR);
A=b.bstrval;
There are a variety of computer languages, such as C + +, Java, Basic, Pascal, etc., as well as JavaScript, VBScript, ActionScript and other scripting languages, each maintaining their own data types when using C + + In such a strongly typed language to read the database or to Exchange data with other languages, it is very likely that it does not know the exact type of data obtained, and this time must be read with the help of variant types. The Variant data type has cross-language characteristics, and it can represent (store) any type of data. Its definition in Visual C + +:
typedef tagvariant VARIANT;
A VARIANT is actually a structure in which a VT member is used to represent the type of data, while the real data is stored in the Union space. In general we use the variant steps as shown below.
Define a Variant variable, such as: var.
The data type of the Variant variable is set by the VT member, such as: VAR.VT = VT_I4.
The data content is set by the corresponding union member, such as: Var.lval = 100.
In summary, a variant is used to represent an integral type of data:
VARIANT var;
VAR.VT = VT_I4; Indicates the integer type data
Var.lval = +; Assign value
Use a Variant to represent a Boolean value:
VARIANT var;
VAR.VT = Vt_bool; Indicates the integer type data
Var.boolval = VARIANT_TRUE; Assign value
Save a string with a variant:
VARIANT var;
VAR.VT = VT_BSTR;
According to the above code, the reader may guess that the variant definition might resemble the following:
struct VARIANT
{
VARTYPE VT; Data type
{
LONG lval; Vt_i4
Variant_bool boolval //vt_bool
BSTR bstrval; Vt_bstr
}
In fact, the definition of a variant is like this! Just because it needs to support too many types, it contains more union members. Confined to space, no longer attached here.
The type supported by the variant, which is the value of the VT member, is shown in table 4-3.
Table 4-3 Variant supported types |
Type name |
Meaning |
Vt_empty |
Indicates that no value is specified |
Vt_null |
Indicates a null value (similar to a null value in SQL) |
Vt_i2 |
Indicates a short integer |
Vt_i4 |
Indicates a long integer |
Vt_r4 |
Indicates the float value |
Vt_r8 |
Indicates a double value |
Vt_cy |
Indicates the currency value |
Vt_date |
Indicates a DATE value |
Vt_bstr |
Indicates a BSTR string |
Vt_dispatch |
Indicates the IDispatch pointer |
Vt_error |
Indication SCODE |
Vt_bool |
Indicates a Boolean value |
Vt_variant |
Indicates the Variantfar pointer |
Vt_unknown |
Indicates the IUnknown pointer |
Vt_decimal |
Indicates a decimal value |
Vt_i1 |
Indicates a char value |
(Continuation of table)
Type name |
Meaning |
Vt_ui1 |
Indicates that byte |
Vt_ui2 |
Indication Unsignedshort |
Vt_ui4 |
Indication Unsignedlong |
Vt_i8 |
Indicates a 64-bit integer |
Vt_ui8 |
Indicates a 64-bit unsigned integer |
Vt_int |
Indicates an integer value |
Vt_uint |
Indicates the unsigned integer value |
Vt_void |
Indicates C-style void |
Vt_hresult |
Indicator HRESULT |
Vt_ptr |
Indicates pointer type |
Vt_safearray |
Indication SAFEARRAY |
Vt_carray |
Indicates a C-style array |
vt_userdefined |
Indicates a user-defined type |
Vt_lpstr |
Indicates a NULL-terminated string |
Vt_lpwstr |
indicated by Nullnothingnullptrnull References (in Visual Basic The wide string terminated by the |
Vt_record |
Indicates a user-defined type |
Vt_filetime |
Indicates the FILETIME value |
Vt_blob |
Indicates bytes prefixed with length |
Vt_stream |
Indicates the name of the stream followed by |
Vt_storage |
Indicates the name that is then stored |
Vt_streamed_object |
Indicates that the stream contains an object |
Vt_stored_object |
Indicates that the store contains objects |
Vt_blob_object |
Indicates that the BLOB contains an object |
Vt_cf |
Indicates the Clipboard format |
Vt_clsid |
Indicates the class ID |
Vt_vector |
Indicates a simple count array |
Vt_array |
Indicates the SAFEARRAY pointer |
Vt_byref |
Indicates that the value is a reference |
.2.5 _variant_t, CComVariant and COleVariant, cdbvariant
From the above can be seen variant this type of use is more complex, in fact, there is a simple way, that is, the use of Variant packaging class _variant_t. The _variant_t constructor accepts data from the base data type as a parameter, and a small part of it is listed below:
_variant_t (
Short SSRC,
VARTYPE vtsrc = Vt_i2
);
_variant_t (
Long Lsrc,
VARTYPE vtsrc = VT_I4
);
_variant_t (
Float FLTSRC
) throw ();
_variant_t (
Double DBLSRC,
VARTYPE vtsrc = VT_R8
);
On the other hand, _variant_t provides a reverse conversion function, such as converting a _variant_t to a short value, which is a small part of the following:
operator short () const;
operator long () const;
operator float () const;
operator double () const;
Thus, it can be seen that the conversion between variant types and basic data types can be easily implemented using _variant_t, such as:
Long L = 123;
_variant_t lval (L);
Long m = lval;
You can also use COleVariant and ccomvariant to simplify the operation of the variant, which is referenced in the following code:
COleVariant v3 = _t ("Hello, world!");
COleVariant v4 = (long) 1999;
CString str = (BSTR) v3.pbstrval;
Long i = v4.lval;
The Variant class is shown in Figure 4-7.
498) this.style.width=498; "Height=190> |
(Click to view larger image) Figure 4-7 Variant class diagram |
In addition, in MFC ODBC programming, we also have access to cdbvariant,cdbvariant no base class, its function is similar to COleVariant, the only difference is that it does not use OLE. As you can see, Visual C + + provides too many wrapper classes for variants, and if feasible, we recommend that readers try to use uniform classes in their own code, such as: _variant_t.
VARIANT, _variant_t, CComVariant, COleVariant, cdbvariant