Solved

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

Posted on 2001-08-30
10
599 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 1

Author Comment

by:ua1zcl
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:ua1zcl
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now