16,472
社区成员
发帖
与我相关
我的任务
分享
_RecordsetPtr CMyJs::Execute(LPCTSTR lpSQL, long lOptions)
{
ASSERT(m_pConnection != NULL);
ASSERT(AfxIsValidString(lpSQL));
try
{
return m_pConnection->Execute(_bstr_t(lpSQL), NULL, lOptions);
}
catch (_com_error e)
{
m_strError = (LPCTSTR)e.Description();
return NULL;
}
}
//截取出来的执行代码
CString strSql;
strSql.Format("EXEC %s %s",mapParam["proname"].c_str(),(LPCTSTR)GetProcedureParam( mapParam ) );
if( CConfig::GetInstance().m_bLogGetUrl)
{
IBLOG(strSql);
}
map<CString,vector<CString> > mapRecordSet;
if( !DBtoMap( strSql,mapRecordSet,adCmdText ) || 0 == mapRecordSet.size() )
{
return FALSE;
}
//这个函数可以把执行结果保存到map里面。支持多条记录集返回
BOOL CMyJs::DBtoMap( CString strSql,map<CString,vector<CString> >& mapRecordSet,long lOption)
{
HRESULT hr;
Fields* pFields = NULL;
long lnColCount;
try
{
m_pRecordset = Execute( (LPCTSTR)strSql,lOption );
size_t nRecordSet = 0;
while( m_pRecordset != NULL )
{
hr = m_pRecordset->get_Fields( &pFields );
if( SUCCEEDED( hr ) )
{
pFields->get_Count( &lnColCount );
pFields->Release();
pFields = NULL;
_variant_t vtFld;
if( m_pRecordset->adoEOF )
{
m_pRecordset->Close();
return FALSE;
}
while( !m_pRecordset->adoEOF )
{
for( long nIndex = 0 ; nIndex < lnColCount; nIndex++ )
{
BSTR bstrColName;
m_pRecordset->Fields->Item[nIndex]->get_Name( &bstrColName );
CString strColName;
strColName.Format("%s_r%d",(CString)bstrColName,nRecordSet);
vtFld = m_pRecordset->Fields->GetItem(nIndex)->Value;
mapRecordSet[strColName].push_back( (VariantToString( vtFld)));
SysFreeString(bstrColName);
}
m_pRecordset->MoveNext();
}
long lRec = 0;
_RecordsetPtr pRecordSet = m_pRecordset;
m_pRecordset = m_pRecordset->NextRecordset( (VARIANT*)lRec );
CloseRecordSet(pRecordSet);
nRecordSet++;
}
}
return TRUE;
}
catch(_com_error &e)
{
m_strError.Format("SQL执行失败!\r\n错误信息:%s SQL:%s",(char*)(e.Description()),(LPCTSTR)strSql);
}
return FALSE;
}