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

x
?
Solved

executing query from mfc

Posted on 2001-09-13
11
Medium Priority
?
202 Views
Last Modified: 2013-11-20
I have the following query:

select item_name from item_table where item_id=33 order by item_date desc

I am using mfc as front end and sql server as back end.I want to connect them using odbc.

Plz give me the detailed solution.

0
Comment
Question by:abalkur
[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
11 Comments
 
LVL 8

Expert Comment

by:VinExpert
ID: 6478701
Hi,

I guess U have the class derived from CRecordSet to handle the table "item_table". Let that class name be CMyTable.

Then to get the resultant record set with specified values, u can use the members of CRecordSet.
use m_strSort for arranging and m_strFilter for setting the where condition. In the above case, it may be as

CMyTable cItemTable;
cItemTable.m_strSort = "item_date";
cItemTable.m_strFilter = "item_id = 33";
if(cItemTable.IsOpen())
  cItemTable.Close();
cItemTable.Open();
//Now cItemTable's entry for item_name will have the name corresponding to item_id = 33.

If more than one records are there with item_id = 33, then the resultant recordset after opening the table will have all the qualified entries. U may have to use a loop checking for IsBOF() or IsEOF() functions.

If still want to execute the SQL queries itself, then u can use ExecuteSQL(...) of CDatabase after forming the query.

Try it out.
VinExpert
0
 

Author Comment

by:abalkur
ID: 6478785
Where do i have to provide "desc" in this.

Thanks & Regards
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6479243
>> Where do i have to provide "desc" in this.
in the m_strSort
m_strSort="item_id desc";
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 6

Expert Comment

by:Triskelion
ID: 6479265
There are a few things you need to do before all of this will work, though.

1) Database should be registered in your ODBC administrator.
2) You should have created a class for the CRecordset in the class wizard binding it to your database table.

3) If you're issuing this query more than once with different criteria, you will need a slightly different approach (using parameters in the query)

4) If you're connecting more than one CRecordset to a database then you should create a CDatabase connection and attach each CRecordset to it using its address.
0
 
LVL 4

Accepted Solution

by:
aponcealbuerne earned 400 total points
ID: 6479658
You could do something like this.

CDatabase* dbData;
CRecordset* rsData;
dbData = new CDatabase();
dbData->OpenEx(_T("DSN=OdbcName;UID=UserId;PWD=pass");

CString strSql;
CString strItemName;
strSql = "select item_name from item_table where item_id=33 order by item_date desc"

rsData = new CRecordset(dbData);
rsData->Open(AFX_DB_USE_DEFAULT_TYPE, strSql);

while(!rsData->IsEOF())
{    
   rsData->GetFieldValue((short)0, strItemName);
   rsData->MoveNext();
}
rsData->Close();
delete rsData;
delete dbData;

Hope helps
0
 
LVL 1

Expert Comment

by:MRNMurthy
ID: 6482785
Hi,

I am just watching.

From

MRN Murthy
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6483308
aponcealbuerne, that's a very rough technique and is of limited scope (not reusable).  The user would always have to know/memorize the schema of the database in order to write the code.  I wouldn't imagine using it in production code HOWEVER, it looks as if it would be OK for experimental code.  

I'm not dogging it for raw functionality, though.

With the Class Wizard, you can choose to bind the columns you need and the framework handles the data types and data transfers.

There are a few other 'goodies' that happen in the database when you derive your class FROM a CRecordset (such as the SQL_PREPARE that happens when you parameterize the CRecordset).
0
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6483402
Triskelion, well it's true what you said, of course you can use class wizard to do so, and I know also about CRecordset methods, but abalkur asked an specific question with a static sql query, I'm giving him only a tip of how he could solve the problem.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6483994
You're right.
I'm making assumptions about the nature and future use of the query.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9532244
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: aponcealbuerne

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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