Click to See Complete Forum and Search --> : getField() with ordinal fails : VC++ (2008 express) and SQL Server (2005 express)


valley
January 30th, 2008, 03:59 PM
Hi,

I'm hoping this hasn't been answered many times before. I couldn't find anything in the search.

I'm using MSVC++ 2008 Express, alongside MS SQL Server 2005 Express, and trying to connect via ADO.

My problem basically is that I'm trying to iterate through a list of fields in a recordset by ordinal index rather than by name.

The recordset was created by an SQL query such as SELECT * FROM mytable, or SELECT field1, field2 FROM mytable.

In both cases, I can see the that the recordset contains records (the correct amount for the select) and those records contain fields (again the correct amount). Further I can access the fields by using:

recordset->GetFields()->GetItem("field1")

but if I try to do:

recordset->GetFields()->GetItem(0)

I get an exception to the effect of the field requested does not exist.

I can't see why this isn't working. The docs on MSDN suggest that iterating by ordinal is acceptable, and I've seen plenty of VB code that does it. I've not seen any C++ code examples that do though, so I might be missing a step.

Can anyone offer any help?

valley
January 30th, 2008, 04:53 PM
And almost immediately after I posted this, I stumbled on the answer.

So for anyone else that may run into this issue, you need to cast your ordinal index as a long, so:

recordset->GetFields()->GetItem(0)

wont work, but:

recordset->GetFields()->GetItem(long(0))

will.

Grrr...