What is GetCollect?

What is GetCollect?

Post by David Lam » Sun, 10 Nov 2002 00:58:23



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.

 
 
 

What is GetCollect?

Post by Roy L Fin » Sun, 10 Nov 2002 10:29:28


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.


 
 
 

What is GetCollect?

Post by David Lam » Wed, 13 Nov 2002 01:28:07


Many thanks
Quote:>-----Original Message-----
>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
Quote:>**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
Quote:>the methods of the class.

>GetCollect is a but a shortcut method that operates

directly on the Fields
Quote:>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
Quote:>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

- Show quoted text -

>and the VARIANT value.  Both parameters are passed in the
helper _variant_t
>class.

>I hope this helps...

>regards
>roy fine



>> 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.

>.

 
 
 

1. _RecordsetPtr::GetCollect(), PutCollect() question

Using VC++6.0 and ADO/COM...

What I'm trying to do is this:  I have a CPropertySheet with a few
CPropertyPage.  Each page contains different properties from a recordset.  I
am filling each page's member variables with the appropriate fields from the
recordset using _RecordsetPtr::GetCollect().  It makes sense to me that in
order to keep the record locked, I should keep the recordset open until the
property sheet is closed (I assume I would also use pessimistic locking as
well).  Also, it makes sense to me that I should be able to 'commit' the
changes made on all pages when the Apply button is pressed.

So I am doing this in a member function (SaveChanges()) in my
CPropertySheet.  By calling this function from any of the CPropertyPages
when the Apply button is pressed, I only have to write one function.  Still
with me???

Anyway, if I write a function that simply "SELECT"s a record, fill a
variable with a variable from the recordset using GetCollect(), modify the
value, and commit the change using _RecordsetPtr::PutCollect() and
_RecordsetPtr::Update(), it works fine.  However, when I SELECT my record
and fill the variable in one function, then change it and try to commit the
change in a different function, I catch a _com_error that says "The
operation requested by the application is not supported by the provider."

Does anyone have any idea why this is not working the way I want it to?
Also, must I keep the recordset open to preserve 'proper' record locking?

If needed, I can send the code...

Thanks,
KO

2. win 2000 and 9.0.1.1 ERROR

3. ADO and GetCollect

4. Toronto, CAN - Oracle DBAs needed for high profile E-Commerce project

5. GetCollect problem

6. SA password error

7. GetCollect type

8. Failing to connect to SQL7

9. GetCollect and Null data

10. I am getting this message when i am tring to export or import anything using

11. error ORA-01855: AM/A.M. or PM/P.M. required

12. Busy Day = Slowdown from 12 AM - 5 AM

13. Use of @am, Am I dumb?