• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 884
  • Last Modified:

C++ / ADO Recordset display (easy points)

Hi all, I know this is easy and have done it before but it's been a long day and i'm having a mental blank.

I need to simply display a table (and headers) in a commmand prompt (printf) using ADO in C++.  Just a simple one like this

Title1   Title2
----------------
1        2
3        4

I'm connected fine and the data is there but I keep getting various errors at runtime.  Everything I try seems to not work for some reason, HELP! (Think my brains finally starting to melt)
0
IeuanJ
Asked:
IeuanJ
  • 2
  • 2
1 Solution
 
may_f_24Commented:
try this:

_RecordsetPtr rs("ADODB.Recordset");
_ConnectionPtr pConn("ADODB.Connection");
//open the connection.....

_bstr_t bSql = "select title1, title2 from X";

rs->Open (bSql, _variant_t((IDispatch *)pConn, true), adOpenForwardOnly, adLockReadOnly, adCmdText);

printf("title1 \tTitle2 \r\n");
printf("------------------------ \r\n");

  _bstr_t bTitle1;
  _bstr_t bTitle2;

while (!rs->EndOfFile)
{
  bTitle1 = rs->Fields->GetItem(0L)->Value;
  bTitle2 = rs->Fields->GetItem(1L)->Value;

  printf ("%s \t%s\r\n", bTitle1.operator char*,  bTitle2.operator char *);
  rs->MoveNext ();
}

// close the recordset and the connection
0
 
may_f_24Commented:
sorry... the lines in the loop shuold be like this:
 bTitle1 = rs->Fields->GetItem(0L)->Value.bstrVal;

this if the value in table is string.
if its a number, then:
 long nTitle1;
....
  nTitle1 = rs->Fields->GetItem(0L)->Value;
....
printf ("%d \t%d\r\n", nTitle1...);
0
 
IeuanJAuthor Commented:
Hello again.  I am getting the numerical values out fine now but the text is coming out as garbage.  Below is what I have.  the counts are correct, so is field1 (numerical) but field2 (varchar) is ending up as crap strings of ascii charachters.

Any Ideas ?



short bTitle1;
_bstr_t bTitle2;

long limit = 0;
long reccount = 0;
limit = ((pRecordSet->Fields->Count));
reccount = ((pRecordSet->RecordCount));
printf("Limit = %d / %d\n",limit, reccount);

while (!pRecordSet->EndOfFile)
{
   bTitle1 = pRecordSet->Fields->GetItem(0L)->Value;
   bTitle2 = pRecordSet->Fields->GetItem(1L)->Value.bstrVal;

   printf ("%d \t%s\r\n", bTitle1,  bTitle2.operator char *);
   pRecordSet->MoveNext ();
}
0
 
IeuanJAuthor Commented:
Got it working now myself from where I was, but you get the points for helping me on the way (and also being the only person kind enough to try).  My answer is below.





 short bTitle1;
 _variant_t      vtFirstName;


    long limit = 0;
    long reccount = 0;
    limit = ((pRecordSet->Fields->Count));
    reccount = ((pRecordSet->RecordCount));
     printf("Limit = %d / %d\n",limit, reccount);

     while (!pRecordSet->EndOfFile)
{
 bTitle1 = pRecordSet->Fields->GetItem(0L)->Value;
 vtFirstName = pRecordSet->Fields->GetItem(1L)->GetValue();
 printf ("%d \t%s\r\n", bTitle1,(char*)((_bstr_t)vtFirstName));

 pRecordSet->MoveNext ();
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now