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语句就可以了.