山寨:不是最好的,是最适合我们的!欢迎体验山寨 中文版MSDN

Blog @ Blog

当华美的叶片落尽,生命的脉络才历历可见。 -- 聂鲁达

常用链接

统计

积分与排名

BBS

Blog

Web

最新评论

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\n"), 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\n"), 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;

posted on 2008-06-17 23:35 isabc 阅读(1077) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


广告信息(免费广告联系)

中文版MSDN:
欢迎体验