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;