David,
GetCollect is missing from all of the ADO documentation - but it is NOT
**undocumented** in the classic sense - in other works, it is perfectly
correct to use it.. The reason for the lack of documentation is that the
**MAJORITY** of the documentation is written to the VB or VBScript audience.
GetCollect and its counterpart, PutCollect are helper methods of the
Recordset class -- they are in the vtable, but not in the automation methods
list. Remember - ADO exposes a dual interface - IDispatch, and then all of
the methods of the class.
GetCollect is a but a shortcut method that operates directly on the Fields
collection in the Recordset. Hence, these two are equivalent, but the
GetCollect is more efficient:
1) _bstr_t fname = rset->Fields->GetItem((short)0).Value;
2) _bstr_t fname = rset->GetCollect((short)0).Value;
GetCollect takes one argument - it is a variant, and it identifies the field
that you are "GET-ing" You can pass the field name, or pass a ordinal
value - but the type of the variable ultimately VARIANT. The arguments are
defined as _variant_t, and if you look in MSDN, you will find there are a
lot of c-tors for the _variant_t class, so specifying an int will cause the
compiler to insert a c-tor/conversion as appropriate. So either of these
will work, given the following sql statement:
"Select fname,lname from names"
1) _bstr_t fname = rset->Fields->GetItem((short)0).Value;
2) _bstr_t fname = rset->Fields->GetItem(L"FNAME").Value;
3) _bstr_t fname = rset->GetCollect((short)0).Value;
4) _bstr_t fname = rset->GetCollect(L"FNAME").Value;
5) BSTR fname = ::SysAllocString(rset->GetCollect((short)0).Value.bstrVal);
The counterpart PutCollect takes two arguments - the field name or ordinal
and the VARIANT value. Both parameters are passed in the helper _variant_t
class.
I hope this helps...
regards
roy fine
Quote:> The book I bought to learn database programming in VC++
> was "Teach Yourself Database programming with Visual C++ 6
> is 21 Days", from SAMS.
> In it, the author is fairly consistent in the way he gets
> data out of records.
> _RecordsetPtr pRS;
> _variant_t v;
> v=pRS->GetCollect(L"fieldname");
> But what is GetCollect()? It works beautifully, but it
> isn't referenced anywhere in the MSDN library or any other
> source I can find. The problem is that if I want to pass
> the argument to GetCollect as a variable, I don't know
> what sort of variable to pass it.
> ie. CString csfieldname;
> csfieldname="fieldname";
> GetCollect(csfieldname.GetBuffer(0));//doesn't work.
> What is that variable? The one where L"fieldname always
> works, but none of the other variations I can think of
> work.
> Or..alternatively.. I want a function which, given a
> recordset and a field name will return a variant that
> contains the value of the field with that field name. I
> use GetCollect for that purpose, but I'm beginning to
> wonder if that is really what I ought to be using.