首页 > 代码库 > recordset记录集方式使用

recordset记录集方式使用

作者:BY  www.gudianxiaoshuo.com


本文由 代码助手软件 整理发布 内容与本软件无关 更惬意的读、更舒心的写、更轻松的发布

 秀色书文软件  可听、可读、可写、可知识挖掘、可标注,再加上史上最强大的纯文本配色功能, 瞬间使您的书文秀色起来。








一 获得每个学生的成绩

    _RecordsetPtr pRecordSet;
    pRecordSet.CreateInstance(_uuidof(Recordset));
    CString recordStr;
    recordStr.Format (_T("Select * from  课堂成绩表%d order by 学号 "),
        m_pParentStudentInfo->m_nXueqiID);
    try{
        pRecordSet->Open(
            (LPCTSTR)recordStr,
            m_pConnection.GetInterfacePtr(),
            adOpenDynamic,
            adLockOptimistic,
            adCmdText
            );
        if (!pRecordSet->adoBOF)
        {
            pRecordSet->MoveFirst ();
        }else
            return; //数据库为空
        CString nXuehaoStr;
        CString nameStr;
        CString youxiuStr;
        CString liangStr;
        CString jiayouStr;
        CString kuangkeStr;
        _variant_t varValue;
        unsigned nCount=0;
        while (!pRecordSet->adoEOF)
        {
            varValue=pRecordSet->GetCollect(_T("学号"));
            if (varValue.vt!=VT_NULL)
            {
                nXuehaoStr=((LPCTSTR)_bstr_t(varValue));
                if (_ttoi(nXuehaoStr)==0) //特殊学生 保存其他信息
                {    
                    pRecordSet->MoveNext ();
                    continue;
                }
            }else{
                pRecordSet->MoveNext ();
                continue;
            }
            varValue=pRecordSet->GetCollect (_T("姓名"));
            if (varValue.vt!=VT_NULL)
            {
                nameStr=(LPCTSTR)_bstr_t(varValue);
            }else
                nameStr=L"";
            varValue=pRecordSet->GetCollect (_T("优秀"));
            if (varValue.vt!=VT_NULL)
            {
                youxiuStr=(LPCTSTR)_bstr_t(varValue);
            }else
                youxiuStr=L"0";
            varValue=pRecordSet->GetCollect (_T("良"));
            if (varValue.vt!=VT_NULL)
            {
                liangStr=(LPCTSTR)_bstr_t(varValue);
            }else
                liangStr=L"0";
            varValue=pRecordSet->GetCollect (_T("一般"));
            if (varValue.vt!=VT_NULL)
            {
                jiayouStr=(LPCTSTR)_bstr_t(varValue);
            }else
                jiayouStr=L"0";
            varValue=pRecordSet->GetCollect (_T("旷课"));
            if (varValue.vt!=VT_NULL)
            {
                kuangkeStr=(LPCTSTR)_bstr_t(varValue);
            }else
                kuangkeStr=L"0";
            m_StuKetangScoreList.InsertItem(nCount,nXuehaoStr);
            m_StuKetangScoreList.SetItemText(nCount,1,nameStr);
             m_StuKetangScoreList.SetItemText(nCount,2,youxiuStr);
             m_StuKetangScoreList.SetItemText(nCount,3,liangStr);
             m_StuKetangScoreList.SetItemText(nCount,4,jiayouStr);
            m_StuKetangScoreList.SetItemText(nCount,5,kuangkeStr);
            nCount++;
            pRecordSet->MoveNext ();
        }
    }
    catch(_com_error & e)
    {
        AfxMessageBox(e.ErrorMessage());
    }

获得班级信息

    _RecordsetPtr pRecordSet;
    pRecordSet.CreateInstance(_uuidof(Recordset));
    CString recordStr;
    recordStr.Format (_T("Select * from  ClassInfo order by 入学年,班级,ID"));
    try{
        pRecordSet->Open(
            (LPCTSTR)recordStr,
            m_pConnection.GetInterfacePtr(),
            adOpenDynamic,
            adLockOptimistic,
            adCmdText
            );
        if (!pRecordSet->adoBOF)
        {
            pRecordSet->MoveFirst ();
        }else
            return; //数据库为空
        CString ruxueNianStr;
        CString banjiStr;
        CString xueqiStr;
        _variant_t varValue;
        unsigned nCount=0;
        int nID=0;
        int banjiID=0;
    
        BOOL bSelected=FALSE;
        short nDianming=0;
        while (!pRecordSet->adoEOF)
        {
            varValue=pRecordSet->GetCollect(_T("点名"));
            if (varValue.vt!=VT_NULL)
            {
                nDianming=_ttoi((LPCTSTR)_bstr_t(varValue));
            }else
                nDianming=0;
            varValue=pRecordSet->GetCollect(_T("入学年"));
            if (varValue.vt!=VT_NULL)
            {
                ruxueNianStr=(LPCTSTR)_bstr_t(varValue);
            }else
                continue;
            varValue=pRecordSet->GetCollect (_T("班级"));
            if (varValue.vt!=VT_NULL)
            {
                banjiStr=(LPCTSTR)_bstr_t(varValue);
            }else
                continue;
            varValue=pRecordSet->GetCollect (_T("学期"));
            if (varValue.vt!=VT_NULL)
            {
                xueqiStr=(LPCTSTR)_bstr_t(varValue);
            }else
                continue;
            m_BanjiList.MyInsertItem(nCount,_T(""),nDianming); //复选框位置名为空  
        
            //m_BanjiList.InsertItem(nCount,ruxueNianStr,1);
            m_BanjiList.SetItemText(nCount,1,ruxueNianStr);
            m_BanjiList.SetItemText(nCount,2,banjiStr);
            m_BanjiList.SetItemText(nCount,3,xueqiStr);
            varValue=pRecordSet->GetCollect(_T("ID"));
            if (varValue.vt!=VT_NULL)
            {
                nID=_ttoi((LPCTSTR)_bstr_t(varValue));
            }else
                continue;
            if (nID==m_pParentStudentInfo->m_nXueqiID)
            {
                bSelected=TRUE;
                m_BanjiList.SetItemState (nCount,LVIS_SELECTED,LVIS_SELECTED);
            }
            varValue=pRecordSet->GetCollect(_T("班级ID"));
            if (varValue.vt!=VT_NULL)
            {
                banjiID=_ttoi((LPCTSTR)_bstr_t(varValue));
            }
            nCount++;
            pRecordSet->MoveNext ();
        }
        if (!bSelected&&nCount>0) //为选择任何项,则默认选择最后一项
        {
            m_BanjiList.SetItemState (nCount-1,LVIS_SELECTED,LVIS_SELECTED);
            
            m_pParentStudentInfo->m_nXueqiID=nID;
            m_pParentStudentInfo->m_nClassID=banjiID;
        }
    }
    catch(_com_error & e)
    {
        AfxMessageBox(e.ErrorMessage());
    }

获得学生信息

    _RecordsetPtr pRecordSet;
    pRecordSet.CreateInstance(_uuidof(Recordset));
    CString recordStr;
    recordStr.Format (_T("Select * from  班级表%d order by 学号 "),
        m_pParentStudentInfo->m_nClassID);
    try{
        pRecordSet->Open(
            (LPCTSTR)recordStr,
            m_pConnection.GetInterfacePtr(),
            adOpenDynamic,
            adLockOptimistic,
            adCmdText
            );
        if (!pRecordSet->adoBOF)
        {
            pRecordSet->MoveFirst ();
        }else
            return; //数据库为空
        CString nXuehaoStr;
        CString nameStr;
        CString addStr;
        CString lianxiStr;
        CString beizhuStr;
        _variant_t varValue;
        unsigned nCount=0;
        while (!pRecordSet->adoEOF)
        {
            varValue=pRecordSet->GetCollect(_T("学号"));
            if (varValue.vt!=VT_NULL)
            {
                nXuehaoStr=((LPCTSTR)_bstr_t(varValue));
                if (_ttoi(nXuehaoStr)==0)
                    continue;
            }else
                continue;
            varValue=pRecordSet->GetCollect (_T("姓名"));
            if (varValue.vt!=VT_NULL)
            {
                nameStr=(LPCTSTR)_bstr_t(varValue);
            }else
                nameStr=L"";
            varValue=pRecordSet->GetCollect (_T("地址"));
            
            if (varValue.vt!=VT_NULL)
            {
                addStr=(LPCTSTR)_bstr_t(varValue);
            }else
                addStr=L"";
            varValue=pRecordSet->GetCollect (_T("联系方式"));
            if (varValue.vt!=VT_NULL)
            {
                lianxiStr=(LPCTSTR)_bstr_t(varValue);
            }else
                lianxiStr=L"";
            varValue=pRecordSet->GetCollect (_T("备注"));
            if (varValue.vt!=VT_NULL)
            {
                beizhuStr=(LPCTSTR)_bstr_t(varValue);
            }else
                beizhuStr=L"";
            m_XueShengList.InsertItem(nCount,nXuehaoStr);
            m_XueShengList.SetItemText(nCount,1,nameStr);
            m_XueShengList.SetItemText(nCount,2,addStr);
            m_XueShengList.SetItemText(nCount,3,lianxiStr);
            m_XueShengList.SetItemText(nCount,4,beizhuStr);
            nCount++;
            pRecordSet->MoveNext ();
        }
    }
    catch(_com_error & e)
    {
        AfxMessageBox(e.ErrorMessage());
    }


recordset记录集方式使用