1.VC執(zhí)行一個(gè)帶參數(shù)的存儲(chǔ)過程,返回一個(gè)記錄集:
_RecordsetPtr m_pRecordSetTemp;
m_pRecordSetTemp.CreateInstance("ADODB.Recordset");
#ifdef _DEBUG
if (m_pRecordSetTemp== NULL)


{
AfxMessageBox("RecordSet 對(duì)象創(chuàng)建失敗! 請(qǐng)確認(rèn)是否初始化了COM環(huán)境.");
}
#endif
_CommandPtr m_pCommand;
m_pCommand.CreateInstance(__uuidof(Command));
#ifdef _DEBUG
if (m_pCommand == NULL)


{
AfxMessageBox("Command 對(duì)象創(chuàng)建失敗! 請(qǐng)確認(rèn)是否初始化了COM環(huán)境.");
}
#endif
ASSERT(m_pCommand != NULL);
//輸入?yún)?shù) Member
_ParameterPtr pParamMember;
pParamMember.CreateInstance("ADODB.Parameter");
pParamMember->Name="member"; //所用存儲(chǔ)過程參數(shù)名稱
pParamMember->Type=adChar; //參數(shù)類型
pParamMember->Size=32; //參數(shù)大小
pParamMember->Direction=adParamInput;//表明是輸入?yún)?shù)
pParamMember->Value=_variant_t(member);
m_pCommand->Parameters->Append(pParamMember);
//執(zhí)行存儲(chǔ)過程
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="GetreMsgFromService"; //存儲(chǔ)過程名稱
m_pCommand->CommandType=adCmdStoredProc;//表示為存儲(chǔ)過程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: 打開記錄集發(fā)生異常. 錯(cuò)誤信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(temp);
}
return m_pRecordSetTemp;
m_pRecordSetTemp->Close();
在這用m_pRecordSetTemp->GetRecordCount()來(lái)獲取記錄條數(shù)沒有用.
2.VC執(zhí)行一個(gè)不帶參數(shù)的存儲(chǔ)過程,返回一個(gè)記錄集:
m_pRecordSet.CreateInstance("ADODB.Recordset");
#ifdef _DEBUG
if (m_pRecordSet == NULL)


{
AfxMessageBox("RecordSet 對(duì)象創(chuàng)建失敗! 請(qǐng)確認(rèn)是否初始化了COM環(huán)境.");
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: 打開記錄集發(fā)生異常. 錯(cuò)誤信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(temp);
}

如果不用存儲(chǔ)過程將sql變量改成sql語(yǔ)句就可以了.
3.VC執(zhí)行一個(gè)帶參數(shù)的存儲(chǔ)過程,返回單個(gè)值:
CString retu;
m_pCommand.CreateInstance("ADODB.Command");
#ifdef _DEBUG
if (m_pCommand == NULL)


{
AfxMessageBox("Command 對(duì)象創(chuàng)建失敗! 請(qǐng)確認(rèn)是否初始化了COM環(huán)境.");
}
#endif
ASSERT(m_pCommand != NULL);
//輸入?yún)?shù) Member
_ParameterPtr pParamMember;
pParamMember.CreateInstance("ADODB.Parameter");
pParamMember->Name="member"; //所用存儲(chǔ)過程參數(shù)名稱
pParamMember->Type=adChar; //參數(shù)類型
pParamMember->Size=32; //參數(shù)大小
pParamMember->Direction=adParamInput;//表明是輸入?yún)?shù)
pParamMember->Value=_variant_t(member);
m_pCommand->Parameters->Append(pParamMember);
//返回值
_ParameterPtr pParamOk;
pParamOk.CreateInstance("ADODB.Parameter");
pParamOk->Name="welcome"; //參數(shù)2名稱
pParamOk->Type=adChar; //字符串
pParamOk->Size=70; //大小為70個(gè)字節(jié)
pParamOk->Direction=adParamOutput; //聲明是輸出參數(shù)
m_pCommand->Parameters->Append(pParamOk);
//執(zhí)行存儲(chǔ)過程
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="GetWelcome"; //存儲(chǔ)過程名稱
m_pCommand->CommandType=adCmdStoredProc;//表示為存儲(chǔ)過程adCmdStoredProc
m_pCommand->Execute(NULL, NULL, adCmdStoredProc);
retu=(char*)_bstr_t(pParamOk->Value);
pParamMember->Release();
return retu;