//假定m_pSet是你的CRecordset派生类.
LONG getsum(CRecordset *m_pSet){
CRecordset aTempSet;
LONG l=0;
if (aTempSet.Open(CRecordset::forwardOnly,"select sum([fieldname]) from "+m_pSet->GetDefaultSQL())){
    CDBVariant dbv;
    aTempSet.GetFieldValue((short)0,dbv);
    if (dbv.m_dwType==DBVT_LONG) l=dbv.m_lVal;
    else if (dbv.m_dwType==DBVT_SHORT) l=dbv.m_iVal;
    else if (dbv.m_dwType==DBVT_STRING) l=_ttoi((LPCTSTR)(*dbv.m_sVal));//针对oracle的返回可能是string处理...
    else ASSERT(FALSE);
}
aTempSet.Close();
return l;
}

解决方案 »

  1.   

    上面有一处写错了.aTempSet.Close()应在括号里面.信手写的,更正得更正点些罢
    LONG getsum(CRecordset *m_pSet/*要统计的记录集*/,LPCTSTR pfldnametosum/*要统计的字段*/){
    CRecordset aTempSet(m_pSet->m_pDatabase);
    LONG l=0;
    CString sqlstr;
    sqlstr.Format("select sum([%s]) from %s",
    pfldnametosum,
    m_pSet->GetDefaultSQL());if (aTempSet.Open(CRecordset::forwardOnly,sqlstr){
        CDBVariant dbv;
        aTempSet.GetFieldValue((short)0,dbv);
        if (dbv.m_dwType==DBVT_LONG) l=dbv.m_lVal;
        else if (dbv.m_dwType==DBVT_SHORT) l=dbv.m_iVal;
        else if (dbv.m_dwType==DBVT_STRING) l=_ttoi((LPCTSTR)(*dbv.m_sVal));//针对oracle的返回可能是string处理...
        else ASSERT(FALSE);
        aTempSet.Close();
    }
    return l;
    }