请教一个ODBC调用存储过程的问题。 如何通过ODBC编程调用存储过程,并且得到存储过程的返回值。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 返回一个记录集:不重载CDatabase db;db.Open(.....);CRecordset rs(&db);rs.Open(CRecordset::forwardOnly ,"{call procname 'aaa',bbb',1}");if (!rs.IsEOF()){...}用MFC ODBC重载crecordset://chcode.hclass chcode : public CRecordset{public: void Move( long nrows, WORD wfetchtype ); chcode(CDatabase* pDatabase = NULL); DECLARE_DYNAMIC(chcode) // Field/Param Data //{{AFX_FIELD(chcode, CRecordset) long m_retreturn_value; CString m_newpassword; CString m_oldpassword; CString m_username; //}}AFX_FIELD// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(chcode) public: virtual CString GetDefaultConnect(); // Default connection string virtual CString GetDefaultSQL(); // Default SQL for Recordset virtual void DoFieldExchange(CFieldExchange* pFX); // RFX support //}}AFX_VIRTUAL// Implementation#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;#endif};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CHCODE_H__FF9F8501_31F2_4794_B697_B7FFB5A15C30__INCLUDED_)//chcode.cpp// chcode.cpp : implementation file//#include "stdafx.h"#include "chcode.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// chcodevoid AFXAPI RFX_Textout(CFieldExchange * pfx, LPCTSTR szname,CString& value, int nmaxlength, int ncolumntype, short nscale);IMPLEMENT_DYNAMIC(chcode, CRecordset)chcode::chcode(CDatabase* pdb) : CRecordset(pdb){ //{{AFX_FIELD_INIT(chcode) m_oldpassword=""; m_newpassword=""; m_username=""; //}}AFX_FIELD_INIT m_nDefaultType = snapshot; m_nParams=4; }CString chcode::GetDefaultConnect(){ return _T("ODBC;DSN=");}CString chcode::GetDefaultSQL(){ return _T("");}void chcode::DoFieldExchange(CFieldExchange* pFX){ //{{AFX_FIELD_MAP(chcode) pFX->SetFieldType(CFieldExchange ::outputParam); //set the field type to outputParam for the return value RFX_Long(pFX, _T("return_value"), m_retreturn_value); //bind the return value to the variable pFX->SetFieldType(CFieldExchange ::inputParam); //reset the field type to inputParam RFX_Text(pFX, "@old", m_oldpassword);//,255,SQL_CHAR,0); RFX_Text(pFX, "@new", m_newpassword);//,255,SQL_CHAR,0); //call the new rfx_Text to get the character output params RFX_Text(pFX, "@loginame", m_username);//,255,SQL_CHAR,0); //}}AFX_FIELD_MAP}/////////////////////////////////////////////////////////////////////////////// chcode diagnostics#ifdef _DEBUGvoid chcode::AssertValid() const{ CRecordset::AssertValid();}void chcode::Dump(CDumpContext& dc) const{ CRecordset::Dump(dc);}#endif //_DEBUG//Move(long nRows, WORD wFetchType)void chcode::Move(long nrows, WORD wfetchtype){ if (m_nFields) CRecordset ::Move(nrows, wfetchtype); else m_bBOF = m_bEOF = true;}调用:CDatabase db1; s1.Format("ODBC;UID=sa;PWD=%s",""); db1.Open("report",false,false,s1); chcode chrs(&db1); //CRecordset rs(&db1); chrs.m_newpassword=in.m1; chrs.m_oldpassword=s3; chrs.m_username="report"; chrs.Open( AFX_DB_USE_DEFAULT_TYPE ,_T("{?=CALL sp_password(?,?,?)}")); //chrs.Open(AFX_DB_USE_DEFAULT_TYPE,"{call sp_password('report','report','report')}"); chrs.Close(); db1.Close(); if(!((CSuper_MarketDoc*)GetDocument())->m_db.CanTransact()) return; if(!((CSuper_MarketDoc*)GetDocument())->m_db.BeginTrans()) return; try { ch="EXEC p_KcspAdd \'"+m_pRsSd->m_sd_bh+"\'"; ((CSuper_MarketDoc*)GetDocument())->m_db.ExecuteSQL(ch); ((CSuper_MarketDoc*)GetDocument())->m_db.CommitTrans(); } catch(CDBException* e) { ((CSuper_MarketDoc*)GetDocument())->m_db.Rollback(); AfxMessageBox(e->m_strError); e->Delete(); return; } 使用“*.sskde” 皮肤当鼠标在编辑框右键时会崩溃 CSDN 开始玩浮动广告了 BC制作的DLL,VC调用的问题. mfc真的公开了所有的源代码么? 用哪些COM+管理接口,可以生成一个新的COM+应用程序(比如服务器应用程序),取名随便ABC吧,内详 请指点vc++中的类COleClientDoc是干什么用的? 我初学VC,请教数据库连接的问题 对话框程序初始时窗口隐藏的,可是托盘上图标右击时菜单显示不出来。 如何判断那个Click事件调用了函数 如何做个批处理连续打印 Bezier曲线的问题,高分求教!! 在VB中可以调用的方法为什么不能在ASP中使用,是什么原因呢?
CDatabase db;
db.Open(.....);
CRecordset rs(&db);
rs.Open(CRecordset::forwardOnly ,"{call procname 'aaa',bbb',1}");
if (!rs.IsEOF()){
...
}用MFC ODBC
重载crecordset:
//chcode.h
class chcode : public CRecordset
{
public:
void Move( long nrows, WORD wfetchtype );
chcode(CDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(chcode)
// Field/Param Data
//{{AFX_FIELD(chcode, CRecordset)
long m_retreturn_value;
CString m_newpassword;
CString m_oldpassword;
CString m_username;
//}}AFX_FIELD
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(chcode)
public:
virtual CString GetDefaultConnect(); // Default connection string
virtual CString GetDefaultSQL(); // Default SQL for Recordset
virtual void DoFieldExchange(CFieldExchange* pFX); // RFX support
//}}AFX_VIRTUAL// Implementation
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CHCODE_H__FF9F8501_31F2_4794_B697_B7FFB5A15C30__INCLUDED_)
//chcode.cpp
// chcode.cpp : implementation file
//#include "stdafx.h"
#include "chcode.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// chcode
void AFXAPI RFX_Textout(CFieldExchange * pfx, LPCTSTR szname,
CString& value, int nmaxlength, int ncolumntype, short nscale);
IMPLEMENT_DYNAMIC(chcode, CRecordset)chcode::chcode(CDatabase* pdb)
: CRecordset(pdb)
{
//{{AFX_FIELD_INIT(chcode)
m_oldpassword="";
m_newpassword="";
m_username="";
//}}AFX_FIELD_INIT
m_nDefaultType = snapshot;
m_nParams=4; }
CString chcode::GetDefaultConnect()
{
return _T("ODBC;DSN=");
}CString chcode::GetDefaultSQL()
{
return _T("");
}void chcode::DoFieldExchange(CFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(chcode)
pFX->SetFieldType(CFieldExchange ::outputParam); //set the field type to outputParam for the return value
RFX_Long(pFX, _T("return_value"), m_retreturn_value); //bind the return value to the variable
pFX->SetFieldType(CFieldExchange ::inputParam); //reset the field type to inputParam
RFX_Text(pFX, "@old", m_oldpassword);//,255,SQL_CHAR,0);
RFX_Text(pFX, "@new", m_newpassword);//,255,SQL_CHAR,0); //call the new rfx_Text to get the character output params
RFX_Text(pFX, "@loginame", m_username);//,255,SQL_CHAR,0);
//}}AFX_FIELD_MAP
}/////////////////////////////////////////////////////////////////////////////
// chcode diagnostics#ifdef _DEBUG
void chcode::AssertValid() const
{
CRecordset::AssertValid();
}void chcode::Dump(CDumpContext& dc) const
{
CRecordset::Dump(dc);
}
#endif //_DEBUG
//Move(long nRows, WORD wFetchType)
void chcode::Move(long nrows, WORD wfetchtype)
{
if (m_nFields)
CRecordset ::Move(nrows, wfetchtype);
else
m_bBOF = m_bEOF = true;
}
调用:CDatabase db1;
s1.Format("ODBC;UID=sa;PWD=%s","");
db1.Open("report",false,false,s1);
chcode chrs(&db1);
//CRecordset rs(&db1);
chrs.m_newpassword=in.m1;
chrs.m_oldpassword=s3;
chrs.m_username="report";
chrs.Open( AFX_DB_USE_DEFAULT_TYPE ,_T("{?=CALL sp_password(?,?,?)}"));
//chrs.Open(AFX_DB_USE_DEFAULT_TYPE,"{call sp_password('report','report','report')}");
chrs.Close();
db1.Close();
return;
if(!((CSuper_MarketDoc*)GetDocument())->m_db.BeginTrans())
return;
try
{
ch="EXEC p_KcspAdd \'"+m_pRsSd->m_sd_bh+"\'";
((CSuper_MarketDoc*)GetDocument())->m_db.ExecuteSQL(ch);
((CSuper_MarketDoc*)GetDocument())->m_db.CommitTrans();
}
catch(CDBException* e)
{
((CSuper_MarketDoc*)GetDocument())->m_db.Rollback();
AfxMessageBox(e->m_strError);
e->Delete();
return;
}