Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Recordset by SQL in MFC, from record to record ...

Posted on 2001-08-30
10
Medium Priority
?
618 Views
Last Modified: 2013-11-25
The question about MFC C++ or MS Windows C++ for
Database managment.
For example: You have database ( MDB or SQL )
1) How to open recordset by SQL in MFC C++?
2) If recordset open, then how to go from record to record to update recordset?
3) How to delete, insert, select recordset in C++.
Please propose a web link to examle or something else.
Alex.
0
Comment
Question by:ua1zcl
10 Comments
 
LVL 1

Accepted Solution

by:
mupchu777 earned 400 total points
ID: 6442997
Well Im going to give you three solutions here:

1st:  You can use OLEDB consumer templates

I'd give code but it would fill a couple of pages...

http://www.msdn.microsoft.com

Just look at the msdn site and look for
FAQ: Using the OLE DB Consumer Templates

Most of your questions can be answered there.  You may want to spend sometime at msdn looking at what they are as well (basically a templated class that simplifies getting data with OLEDB).  

2nd:  You can use ADO

You really only have to use the #import command on the ado library (msado15.dll).  Just remember to change eof to EOF

see http://www.devx.com/free/mgznarch/vcdj/1998/jan98/ado2.asp

and also Articles Q169496 and Q174565 in the Microsoft Knowledge Base

3rd You can use DAO (I don't recommend it)

MFC has its own DAO classes for dealing with recordsets and such.

The basic gist of the way to read data is:

// open database
CDaoDatabase oDb;
CDaoRecordset oRs(&oDb);

oDb.Open(strDataPath); // path to database
               
// execute query
oRs.Open(dbOpenDynaset,"SELECT * from Logs", dbOpenDynaset); // the last parameter determines if you can edit

if (!oRs.IsEOF())
     oRs.MoveFirst();

while (!(oRs.IsEOF()))
{
     COleVariant vValue1;
     COleVariant vValue2;
     COleVariant vValue3;

     // timestamp
     vValue1.vt = VT_DATE;
     oRs.GetFieldValue("EnterTime", vValue1);
     vValue2.vt = VT_BSTR;
     oRs.GetFieldValue("Type", vValue2);
     vValue3.vt = VT_BSTR;
     oRs.GetFieldValue("Msg", vValue3);

     if (vValue1.vt != VT_DATE)
     vValue1.ChangeType(VT_DATE);

     ...
     ...

     etc

     oRs.MoveNext();
}

oRs.Close();
oDb.Close();

also to edit and update a record use the Edit, Update and AddNew member functions of the recordset (note Update must be called after Edit and AddNew to update the record)


hope this helps
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6443093
What about MFC's CDatabase and CRecordset classes?  

That way, you can use ClassWizard's support for creating the DB classes for you and binding the columns to data members.  The CRecordset contains methods to easily Insert, Edit, and Delete rows in a database.  Not the fastest at runtime, but pretty quick to implement.

-=- James.
0
 
LVL 1

Author Comment

by:ua1zcl
ID: 6444140
Mupchu777 and James(jtwine), thank to both. I not accept
any answer until few days let me see for all i got.
Excuse me, but i must rejcet Mupchu777 answer for some time, that is e-e recomendations.
 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:ua1zcl
ID: 6444147
Mupchu777 and James(jtwine), thank to both. I not accept
any answer until few days let me see for all i got.
Excuse me, but i must rejcet Mupchu777 answer for some time, that is e-e recomendations.
 
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6449239
ua1zcl,
I agree with jtwine that CRecordset/CDatabase is the easiest way to get started.  Would you like a step-by-step instruction guide?

-- Dan
0
 
LVL 1

Author Comment

by:ua1zcl
ID: 6453323
I accept mupchu777  answer. Thanks a lot.
I can't find a button to press to accept an answer
for now.
0
 
LVL 1

Expert Comment

by:mupchu777
ID: 6454701
I think if I make another comment you can accept that as an answer.  You can't accept my old answer as you have already rejected it.

I would also clarify that yes CDatabase and CRecordset are options as well.  It is syntaticly similar to CDAODatabase and CDAORecordset.  Personally I would avoid the MFC classes and use ADO (through the import command), but that is just my preference.  The only thing I could really argue is the programming model of ADO coupled with a fairly tight wrapper with little overhead.

0
 
LVL 1

Author Comment

by:ua1zcl
ID: 6455450
I am new in data base programming and can not
anythig argue against. So, thank you , hope meet you again here.
Alex.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6865627
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "mupchu777" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6886446
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

877 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