Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-08-30
10
Medium Priority
?
614 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
[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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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 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.

730 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