Solved

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

Posted on 2001-08-30
10
602 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

22 Experts available now in Live!

Get 1:1 Help Now