?
Solved

C++ / ADO Recordset display (easy points)

Posted on 2003-03-18
4
Medium Priority
?
882 Views
Last Modified: 2012-08-13
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
Comment
Question by:IeuanJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:may_f_24
ID: 8165446
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
 
LVL 1

Accepted Solution

by:
may_f_24 earned 90 total points
ID: 8165568
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
 
LVL 2

Author Comment

by:IeuanJ
ID: 8166587
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
 
LVL 2

Author Comment

by:IeuanJ
ID: 8167350
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question