C++连接数据库的时候怎么调用存储过程
展开全部
给个例子给你看下:
...
_CommandPtr m_cmdptr;
m_cmdptr.CreateInstance(__uuidof( Command ) );
_ParameterPtr inParam2=NULL;
_ParameterPtr inParam3=NULL;
_ParameterPtr inParam4=NULL;
_ParameterPtr outParam1=NULL;
_ParameterPtr outParam2=NULL;
_ParameterPtr outParam3=NULL;
inParam2.CreateInstance(__uuidof(Parameter));
inParam3.CreateInstance(__uuidof(Parameter));
inParam4.CreateInstance(__uuidof(Parameter));
outParam1.CreateInstance(__uuidof(Parameter));
outParam2.CreateInstance(__uuidof(Parameter));
outParam3.CreateInstance(__uuidof(Parameter));
m_cmdptr->ActiveConnection=m_pConnection;
m_cmdptr->CommandType=adCmdStoredProc;
m_cmdptr->CommandText=_bstr_t("proc_XXX");//存储过程的名称
//输入参数
inParam2=m_cmdptr->CreateParameter(_bstr_t("@P1"),adVarChar,adParamInput,32,_bstr_t(P1));
m_cmdptr->Parameters->Append(inParam2);
inParam3=m_cmdptr->CreateParameter(_bstr_t("@P2"),adVarChar,adParamInput,20,_bstr_t(P2));
m_cmdptr->Parameters->Append(inParam3);
//输出参数
outParam1=m_cmdptr->CreateParameter("@P4",adInteger,adParamOutput,sizeof(int));
m_cmdptr->Parameters->Append(outParam1);
outParam2=m_cmdptr->CreateParameter("@P5",adInteger,adParamOutput,sizeof(int));
m_cmdptr->Parameters->Append(outParam2);
outParam3=m_cmdptr->CreateParameter(_bstr_t("@P6"),adVarChar,adParamOutput,20,_bstr_t(P6));
m_cmdptr->Parameters->Append(outParam3);
//执行
m_cmdptr->Execute(NULL,NULL,adCmdStoredProc);
//取返回值
long p1=m_cmdptr->Parameters->GetItem("@P4")->Value;
long p2=m_cmdptr->Parameters->GetItem("@P5")->Value;
VARIANT vtP6 =m_cmdptr->Parameters->GetItem("@P6")->Value;
if (vtP6.vt != VT_NULL)
strcpy(tp,(_bstr_t)vtP6);
m_cmdptr.Detach;
...
_CommandPtr m_cmdptr;
m_cmdptr.CreateInstance(__uuidof( Command ) );
_ParameterPtr inParam2=NULL;
_ParameterPtr inParam3=NULL;
_ParameterPtr inParam4=NULL;
_ParameterPtr outParam1=NULL;
_ParameterPtr outParam2=NULL;
_ParameterPtr outParam3=NULL;
inParam2.CreateInstance(__uuidof(Parameter));
inParam3.CreateInstance(__uuidof(Parameter));
inParam4.CreateInstance(__uuidof(Parameter));
outParam1.CreateInstance(__uuidof(Parameter));
outParam2.CreateInstance(__uuidof(Parameter));
outParam3.CreateInstance(__uuidof(Parameter));
m_cmdptr->ActiveConnection=m_pConnection;
m_cmdptr->CommandType=adCmdStoredProc;
m_cmdptr->CommandText=_bstr_t("proc_XXX");//存储过程的名称
//输入参数
inParam2=m_cmdptr->CreateParameter(_bstr_t("@P1"),adVarChar,adParamInput,32,_bstr_t(P1));
m_cmdptr->Parameters->Append(inParam2);
inParam3=m_cmdptr->CreateParameter(_bstr_t("@P2"),adVarChar,adParamInput,20,_bstr_t(P2));
m_cmdptr->Parameters->Append(inParam3);
//输出参数
outParam1=m_cmdptr->CreateParameter("@P4",adInteger,adParamOutput,sizeof(int));
m_cmdptr->Parameters->Append(outParam1);
outParam2=m_cmdptr->CreateParameter("@P5",adInteger,adParamOutput,sizeof(int));
m_cmdptr->Parameters->Append(outParam2);
outParam3=m_cmdptr->CreateParameter(_bstr_t("@P6"),adVarChar,adParamOutput,20,_bstr_t(P6));
m_cmdptr->Parameters->Append(outParam3);
//执行
m_cmdptr->Execute(NULL,NULL,adCmdStoredProc);
//取返回值
long p1=m_cmdptr->Parameters->GetItem("@P4")->Value;
long p2=m_cmdptr->Parameters->GetItem("@P5")->Value;
VARIANT vtP6 =m_cmdptr->Parameters->GetItem("@P6")->Value;
if (vtP6.vt != VT_NULL)
strcpy(tp,(_bstr_t)vtP6);
m_cmdptr.Detach;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询