cplau
asked on
Call a Stored Procedure!
Hi,
Now I am using the CDatabase & CRecordSet classes to manipulate the database.
How can I call a stored procedure in MSSQL server in MFC?
Also, from calling that procedure, a string will be returned! How can I get this string?
Thanks!
Now I am using the CDatabase & CRecordSet classes to manipulate the database.
How can I call a stored procedure in MSSQL server in MFC?
Also, from calling that procedure, a string will be returned! How can I get this string?
Thanks!
You've got to check out the stored procedure wizard on Code Projects. It's great for working with stored procedures in VC++/MFC:
http://www.codeproject.com/database/spcw.asp
http://www.codeproject.com/database/spcw.asp
you can call a stored procedure like this:
db.ExecuteSQL("Stored_Proc edure_Name Param1, Param2,....");
db is an open CDatabase variable.
I don't know how to get the returned value, sorry.
db.ExecuteSQL("Stored_Proc
db is an open CDatabase variable.
I don't know how to get the returned value, sorry.
Here is a useful piece of code. It obtains one value from any SQL statement. This turns out to be useful in all kinds of situations where you don't want to define a formal recordset using the ClassWizard.
//=-=-=-=-=-=-=-=-=-=-=--= -= class Definition
#include <afxdb.h>
class CRsGetOneValue : public CRecordset
{
public:
CRsGetOneValue(CDatabase* pDatabase, LPCSTR szSQL );
CString m_sSQL;
CString m_sRetValue;
virtual void DoFieldExchange(CFieldExch ange* pFX);
CString GetDefaultSQL() {return m_sSQL;};
};
//=-=-=-=-=-=-=-=-=-=-=--= -= class Implementation
CRsGetOneValue::CRsGetOneV alue(CData base* pdb, LPCSTR szSQL )
: CRecordset(pdb), m_sSQL(szSQL)
{
m_sRetValue="";
m_nFields= 1;
m_nDefaultType= snapshot;
}
void CRsGetOneValue::DoFieldExc hange(CFie ldExchange * pFX)
{
pFX->SetFieldType(CFieldEx change::ou tputColumn );
RFX_Text(pFX, "", m_sRetValue);
}
//=-=-=-=-=-=-=-=-=-=-=--= -= typical usage
void CD10WebDlg::OnButton1()
{
CDatabase db;
BOOL fRet= db.Open( "", FALSE, FALSE, "ODBC;DSN=MyDSN;UID=USER;P WD=PASSWOR D", TRUE);
// CString sSQL= "SELECT sNameLast FROM Inquiry";
// CString sSQL= "SELECT COUNT(*) FROM Inquiry";
CString sSQL= "{call OverDueAccts}";
CRsGetOneValue rs( &db, sSQL );
rs.Open( AFX_DB_USE_DEFAULT_TYPE, 0, CRecordset::executeDirect );
TRACE( rs.m_sRetValue + "\r\n" );
}
=-=-=-=-=-=-=-=-=-=-=-
As you can see, to execute a stored procedure, just pass
"{call spName}"
in the ctor (no spaces between { and call). You can also use any SELECT statement. The single returned value will be a string. If you need an int, just use (for instance):
int nRet= atoi( rs.m_sRetValue );
One other note: If you expect the return value to be larger than 255 bytes, then you need to modify the RFX_Text call:
RFX_Text(pFX, "", m_sRetValue, 2048);
-- Dan
//=-=-=-=-=-=-=-=-=-=-=--=
#include <afxdb.h>
class CRsGetOneValue : public CRecordset
{
public:
CRsGetOneValue(CDatabase* pDatabase, LPCSTR szSQL );
CString m_sSQL;
CString m_sRetValue;
virtual void DoFieldExchange(CFieldExch
CString GetDefaultSQL() {return m_sSQL;};
};
//=-=-=-=-=-=-=-=-=-=-=--=
CRsGetOneValue::CRsGetOneV
: CRecordset(pdb), m_sSQL(szSQL)
{
m_sRetValue="";
m_nFields= 1;
m_nDefaultType= snapshot;
}
void CRsGetOneValue::DoFieldExc
{
pFX->SetFieldType(CFieldEx
RFX_Text(pFX, "", m_sRetValue);
}
//=-=-=-=-=-=-=-=-=-=-=--=
void CD10WebDlg::OnButton1()
{
CDatabase db;
BOOL fRet= db.Open( "", FALSE, FALSE, "ODBC;DSN=MyDSN;UID=USER;P
// CString sSQL= "SELECT sNameLast FROM Inquiry";
// CString sSQL= "SELECT COUNT(*) FROM Inquiry";
CString sSQL= "{call OverDueAccts}";
CRsGetOneValue rs( &db, sSQL );
rs.Open( AFX_DB_USE_DEFAULT_TYPE, 0, CRecordset::executeDirect );
TRACE( rs.m_sRetValue + "\r\n" );
}
=-=-=-=-=-=-=-=-=-=-=-
As you can see, to execute a stored procedure, just pass
"{call spName}"
in the ctor (no spaces between { and call). You can also use any SELECT statement. The single returned value will be a string. If you need an int, just use (for instance):
int nRet= atoi( rs.m_sRetValue );
One other note: If you expect the return value to be larger than 255 bytes, then you need to modify the RFX_Text call:
RFX_Text(pFX, "", m_sRetValue, 2048);
-- Dan
hi ranmanor,
Do you have any additional questions? Do any comments need clarification?
-- Dan
Do you have any additional questions? Do any comments need clarification?
-- Dan
no, thanks.
> no, thanks.
Just a thought: now might be the time to accept DanRollins' comment as an answer... :)
-=- James.
Just a thought: now might be the time to accept DanRollins' comment as an answer... :)
-=- James.
true.
tell it to cplau .
tell it to cplau .
D'oh!
Got confused for a second there...! :O
-=- James.
Got confused for a second there...! :O
-=- James.
hi ranmanor,
Sorry!
hi jtwine,
Sorry!
hi cplau,
Do you have any additional questions? Do any comments need clarification?
-- Dan
Sorry!
hi jtwine,
Sorry!
hi cplau,
Do you have any additional questions? Do any comments need clarification?
-- Dan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Comment from DanRollins accepted as answer.
Thank you
Computer101
Community Support Moderator
Thank you
Computer101
Community Support Moderator
-=- James.