vc++中,用ado执行sql server存储过程,并且取得返回值的例子

类别:VC语言 点击:0 评论:0 推荐:

 _variant_t RecordsAffected;
 string szSql = (string)"P_KS_ADDUSER '" + m_szName+ "','" + m_szPasswd +"';";
 try
 {
  _CommandPtr ptrCmd;
  ptrCmd.CreateInstance(__uuidof(Command));
  ptrCmd->ActiveConnection = pContext->GetConnection();

  VARIANT vtRoyalty;

  vtRoyalty = COleVariant((long)0);
  ptrCmd->Parameters->Append(
   ptrCmd->CreateParameter("Return", adInteger, adParamReturnValue,sizeof(int),vtRoyalty));

  vtRoyalty = COleVariant(m_szName.c_str());
  ptrCmd->Parameters->Append(
   ptrCmd->CreateParameter("Name", adChar, adParamInput,m_szName.size(),vtRoyalty));

  vtRoyalty = COleVariant(m_szPasswd.c_str());
  ptrCmd->Parameters->Append(
   ptrCmd->CreateParameter("Pass", adChar, adParamInput,m_szPasswd.size(),vtRoyalty));

  ptrCmd->CommandText = "P_KS_ADDUSER";
  ptrCmd->CommandType = adCmdStoredProc;
  ptrCmd->Execute(NULL,NULL,adCmdStoredProc);

  cout<<"添加用户成功,id="<<(unsigned int)ptrCmd->Parameters->GetItem("Return")->GetValue()<<endl;
  //cout<<"add id:"<<(unsigned int)ptrCmd->GetParameters()->GetItem(0)<<endl;
  //pContext->GetNetworkSender()->SendPack();
 }
 catch(_com_error e)
 {
  cout<<"增加用户的时候发生异常:"<<(LPCSTR)e.Description()<<endl;
  return;
 }


要是有多个值要返回,那么可以使用输出参数(output parameter)的方法;要是需要返回一个数组,可以使用返回数据集的方法。

本文地址:http://com.8s8s.com/it/it226.htm