Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having trouble extracting the value of a double-precision floating point number from an ADODB recordset. I want to assign the value to a local variable in my C++ routine. I've tried as follows.

C++
double dblTemp = rst->Fields->GetItem("DoubleValue")->Value;


and I've also tried:
C++
double dblTemp = rst->Fields->Item("DoubleValue")->Value;


But neither works. Does anyone know how to do this and can share a code example? I've tried looking at the article by Amit Dey entitled "ATL COM and ADO," but I need an example with a double type variable.

Please let me know.

Thanks,
Carl Olen
Posted
Updated 19-Sep-11 11:29am
v3
Comments
Richard MacCutchan 14-Sep-11 10:00am    
But it doesn't work
This is probably the most useless problem description ever invented. Please, explain what you expect to see returned and what you actually see. Are you sure all the pointers in your expression point to a valid record, etc.

Try Item instead.
C++
rst->Fields->Item["DoubleValue"]->Value 
 
Share this answer
 
I post this code but i cannot test it, because i cant establish an ADODB connection on my laptop. Its a long time ago i wrote this. This code makes a snapshot of a recordset.
C++
RecordSetSnapshot::RecordSetSnapshot(ADODB::_RecordsetPtr* rset)
{
  long          count;
  unsigned int  ix,iy;
  long          colrow[2];

  _rows = 0;
  _cols = (*rset)->Fields->Count;

  try
  {

    _variant_t  fields = vtMissing;
    _variant_t  data   = (*rset)->GetRows(ADODB::adGetRowsRest,(long)ADODB::adBookmarkCurrent,fields);
    _variant_t  value;

    if( (VT_EMPTY!=data.vt) && (S_OK==::SafeArrayGetUBound(data.parray,2,&count)) )
    {
      _rows = 1 + count;

      for(ix=0;ix<_cols;ix++)
      {
        ADODB::Field*    field = 0;

        (*rset)->Fields->get_Item(_variant_t((long)ix),&field);
        // store the field name here: field->Name;

        field->Release();
      }

      for(ix=0;ix<_cols;ix++)
      {
        colrow[0] = ix;
        for(iy=0;iy<_rows;iy++)
        {
          colrow[1] = iy;
          if(S_OK==::SafeArrayGetElement(data.parray,colrow,&value))
          {
            // BSTR or something else. VT_R4 (is double)
            VariantChangeType(&value,&value,0,VT_BSTR);
            // store the value
            VariantClear(&value);
          }
        }
      }
    }

    VariantClear(&data);
    VariantClear(&fields);

  }
  catch(_com_error e)
  {
    TRACE(_S("Error: %s\r\n"),(TCHAR*)e.Description());
    Clear();
  }
  catch(...)
  {
    Clear();
  }

}

Hope this helps.
Regards.
 
Share this answer
 
v2
Here's one solution that worked and I'm continuing to develop with other ideas I've received. Thanks!

double dblTemp;
FieldsPtr fields;
FieldPtr field;
VARIANT D_Value;
VariantInit(&D_Value);
D_Value.vt = VT_R8;
VARIANT Index;
VariantInit(&Index);
Index.vt = VT_I4;
Index.lVal = 1; // index of the field with the double value to be retreived.

HRESULT hr=rst->get_Fields(&fields);
if(SUCCEEDED(hr)) {
hr=fields->get_Item(Index, &field);
}
if(SUCCEEDED(hr)) {
hr=field->get_Value(&D_Value);
}
dblTemp = D_Value.dblVal;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900