Solved

how to run stored procedure from mfc?

Posted on 2001-09-10
10
196 Views
Last Modified: 2013-11-20
The requirement is as follows.

1) say  x=id retrieved from one location.

2) validate password.

3)on successful validation

3.a) select status from table1 where id=x order by date1 desc

3.b) if recordcount<1
insert into table1(id,date1,status,location)
values(x,getdate(),'IN','blr')
else
if  status='IN'
y='out'
else y='IN'
end if

insert into  table1(id,date1,status,location)
values(x,getdate(),y,'blr')

end if

4) on failure get back original screen

database is in sqlserver.

front end mfc

Please tell me the way to sort out this problem.

0
Comment
Question by:rajanna
10 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 6470867
The answer to your question is EXTREMELY involved.
Where have you started?
What does your stored procedure do?

A lot of what you want will be handled by the
   CDatabase and CRecordset classes.

Start here.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_crecordset.3a3a.open.asp
http://die.geocity.de/willemer/windows/mfc.htm

Here is a code sample:
// Construct a CDelinquents recordset object
CDelinquents rsDel( NULL );
CString strSQL = "{CALL Delinquent_Accts (?)}"
// Specify a parameter value (obtained earlier from the user)
rsDel.m_lDistParam = lDistrict;
// Open the recordset and run the query
if( rsDel.Open( CRecordset::snapshot, strSQL ) )
    // Use the recordset ...

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6472219
I don't see any need to run a stored procedure here.  It looks like simple recordset access.  Is running a stored proceduret a requirement of your homework?

-- Dan
0
 

Author Comment

by:rajanna
ID: 6473168
Dear Dan,
 I am a beginner in database manipulation.So please guide me how to solve the above problem without using stored procedures(if it is not required).

Regards
rajanna
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6473349
First, check into the MDSN help on CRecordset and CDatabase.  Use the ClassWizard to create a CRecordset that accesses table1 in your database.

Get your code to where you can open the recordset and read a series of records.  Then learn how to Insert a record.  Once all of this is in place, I can provide a useful class that will allow you to grab any single piece of data from the database.  That will help you with steps 1 and 2 (and elsewhere).

If you want a step-by-step, I'll need you to increase the points to 200.

-- Dan
0
 

Author Comment

by:rajanna
ID: 6474211
Dear Dan

I have increased the points to 200.Please give me the step by step solution.

Regards
Rajanna
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 49

Expert Comment

by:DanRollins
ID: 6476016
First, in order to access your database, you will need a DSN.  This you create using the ODBC (or Data Sources) applet in the Windows Control Panel.  If you need help doing this, just ask.  

Use the name: MyTestData
=-=-=-=-=-
Next, back in C++, create a skeleton app -- dialog based in order to explore MFC features:

1) Use Class Wizard to create an MFC AppWizard (exe).  Set the name to MyTest and click Next.

2) Choose Dialog-based and click [Finish].

3) Use the button tool to create a button named Button1.  Double click it and choose OK.  You are now in the source code editor.

4) At this point you could build and run the program, but it does nothing.

=-=-=-=-=-=-
Next, let's create a class to enable access to your data.

5) Press Ctrl+W to bring up the ClassWizard.
6) Click [Add Class..] and [New...]
7) Set Name to CRsMyTable.  Set Base class to CRecordset.  Click OK
8) If prompted, supply a database login (user name and password) and OK.  
9) Choose a table from the list.  I'll assume that you choose a table named MyTable.  Click OK
10) You are back at the ClassWizard.  Click OK because we can use all defaults.  

The class CRsMyTable now contains a variable for each column in the table.  For instance, for the column named status there is a variable named m_status.

...continued...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6476052
Now, in the source code editor, open the file named StdAfx.h and add this line to enable support for using recordsets:

#include <afxdb.h>
 

Now, open the MyTestDlg.cpp file and scroll down to the code like:

void CMyTestDlg::OnButton2()
{
   // TODO: Add your control notification handler code here
}

Insert this above it (or near the top of the file):

#include "RsMyTable.h"

(this lets you access the CRsMyTable class).  Now modify the code so that it looks like this:

void CMyTestDlg::OnButton2()
{
    CRsMyTable rs;
    BOOL fRet= rs.Open();
    if (!fRet) {
        MessageBox("Error opening recordset" );
        return;
    }
    while( ! rs.IsEOF() ) {
        // use: m_sSomeColumnNameFromYourDatabase
        TRACE( rs.m_city +"\n" );
        rs.MoveNext();
    }
}

Build and run the program.  Watch the "Debug" panel of the Output window.  It will list one item from each record in the database.

When this is all working, post back here.  I'll continue the step-by-step then.

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6496291
hi rajanna,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 6515992
hi rajanna,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6703226
Comment from DanRollins accepted as answer.

Thank you
Computer101
Community Support Moderator
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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 28
Add content to output file 4 62
modThree challenge 4 76
abstract class with all non abstract mentods 6 75
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

939 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