VC执行存储过程

1.VC执行一个带参数的存储过程,返回一个记录集:

 

_RecordsetPtr m_pRecordSetTemp;
 m_pRecordSetTemp.CreateInstance(
"ADODB.Recordset");
 #ifdef _DEBUG
 
if (m_pRecordSetTemp== NULL)
 
{
   AfxMessageBox(
"RecordSet 对象创建失败! 请确认是否初始化了COM环境.");
 }

 
#endif
 _CommandPtr  m_pCommand;
 m_pCommand.CreateInstance(__uuidof(Command));
 #ifdef _DEBUG
 
if (m_pCommand == NULL)
 
{
  AfxMessageBox(
"Command 对象创建失败! 请确认是否初始化了COM环境.");
 }

 
#endif
 ASSERT(m_pCommand 
!= NULL);
  
//输入参数 Member
 _ParameterPtr pParamMember;
 pParamMember.CreateInstance(
"ADODB.Parameter");
 pParamMember
->Name="member";  //所用存储过程参数名称
 pParamMember->Type=adChar; //参数类型
 pParamMember->Size=32;  //参数大小
 pParamMember->Direction=adParamInput;//表明是输入参数
 pParamMember->Value=_variant_t(member);
 m_pCommand
->Parameters->Append(pParamMember);
   
//执行存储过程
 m_pCommand->ActiveConnection=m_pConnection;
 m_pCommand
->CommandText="GetreMsgFromService";  //存储过程名称
 m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc

 
int  recordcount;
 
try
 
{
  m_pRecordSetTemp
=m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
  
while(!m_pRecordSetTemp->adoEOF)
  
{
   AfxMessageBox((LPCTSTR)(_bstr_t)m_pRecordSetTemp
->GetCollect("reMsg"));
   m_pRecordSetTemp
->MoveNext();
   recordcount
=recordcount+1;
  }

 }

 
catch(_com_error e)
 
{
  CString temp;
  temp.Format(_T(
"Warning: 打开记录集发生异常. 错误信息: %s; 文件: %s; 行: %d "), e.ErrorMessage(), __FILE__, __LINE__);
  AfxMessageBox(temp);
 }

 
return m_pRecordSetTemp;
 m_pRecordSetTemp
->Close();

 在这用m_pRecordSetTemp->GetRecordCount()来获取记录条数没有用.

 

2.VC执行一个不带参数的存储过程,返回一个记录集:

 

m_pRecordSet.CreateInstance("ADODB.Recordset");
 #ifdef _DEBUG
 
if (m_pRecordSet == NULL)
 
{
  AfxMessageBox(
"RecordSet 对象创建失败! 请确认是否初始化了COM环境.");
  
return;
 }

 
#endif
 ASSERT(m_pRecordSet 
!= NULL);
 CString sql
="TestGet";
 
int i,recordcount;
 
try
 
{
  m_pRecordSet
->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdStoredProc);
  recordcount
=m_pRecordSet->GetRecordCount();//Get records total. 
  if(!m_pRecordSet->adoEOF)
  
{
   
for(i=0;i<recordcount;i++)
   
{
    AfxMessageBox((LPCTSTR)(_bstr_t)m_pRecordSet
->GetCollect("Account"));
    m_pRecordSet
->MoveNext();
   }

  }

  m_pRecordSet
->Close();
 }

 
catch(_com_error e)
 
{
  CString temp;
  temp.Format(_T(
"Warning: 打开记录集发生异常. 错误信息: %s; 文件: %s; 行: %d "), e.ErrorMessage(), __FILE__, __LINE__);
  AfxMessageBox(temp);
 }

如果不用存储过程将sql变量改成sql语句就可以了.

3.VC执行一个带参数的存储过程,返回单个值:

 

CString retu;
 m_pCommand.CreateInstance(
"ADODB.Command");
 #ifdef _DEBUG
 
if (m_pCommand == NULL)
 
{
  AfxMessageBox(
"Command 对象创建失败! 请确认是否初始化了COM环境.");
 }

 
#endif
 ASSERT(m_pCommand 
!= NULL);
 
//输入参数 Member
 _ParameterPtr pParamMember;
 pParamMember.CreateInstance(
"ADODB.Parameter");
 pParamMember
->Name="member";  //所用存储过程参数名称
 pParamMember->Type=adChar; //参数类型
 pParamMember->Size=32;  //参数大小
 pParamMember->Direction=adParamInput;//表明是输入参数
 pParamMember->Value=_variant_t(member);
 m_pCommand
->Parameters->Append(pParamMember);
 
//返回值
 _ParameterPtr pParamOk;  
 pParamOk.CreateInstance(
"ADODB.Parameter");
 pParamOk
->Name="welcome";  //参数2名称
 pParamOk->Type=adChar;  //字符串
 pParamOk->Size=70;   //大小为70个字节
 pParamOk->Direction=adParamOutput; //声明是输出参数
 m_pCommand->Parameters->Append(pParamOk);
  
//执行存储过程
 m_pCommand->ActiveConnection=m_pConnection;
 m_pCommand
->CommandText="GetWelcome";  //存储过程名称
 m_pCommand->CommandType=adCmdStoredProc;//表示为存储过程adCmdStoredProc
 m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
 retu
=(char*)_bstr_t(pParamOk->Value);
 pParamMember
->Release();
 
return retu;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值