题库管理系统(部分代码)

来源:互联网 发布:成套设备报价软件 编辑:程序博客网 时间:2024/06/05 01:27
//部分添加管理信息相关代码:_RecordsetPtrpRentRecordset;pRentRecordset.CreateInstance(_uuidof(Recordset));HRESULThr;_variant_tvar;CStringstrValue;try{hr = pRentRecordset->Open("SELECT * FROM management",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);if (SUCCEEDED(hr)){//查找有无同名的用户名while (!pRentRecordset->adoEOF){var = pRentRecordset->GetCollect((long)0);if (var.vt != VT_NULL){strValue = (LPCSTR)_bstr_t(var);}strValue.TrimRight();//转换成大写,因为SQL不分大小写CString str = m_Acc;strValue.MakeUpper();str.MakeUpper();if (strValue == str){MessageBox("已有该用户,请换新用户名/n(不分大小写)","消息提示");pRentRecordset->Close();return;}pRentRecordset->MoveNext();}pRentRecordset->AddNew();pRentRecordset->PutCollect("M_Account",_variant_t(m_Acc));pRentRecordset->PutCollect("M_Password",_variant_t(m_Pas));pRentRecordset->PutCollect("M_Competence",_variant_t(m_Comp));if (m_Tim!=""){pRentRecordset->PutCollect("M_Time",_variant_t(m_Tim));}pRentRecordset->Update();}}catch (_com_error  *e){AfxMessageBox(e->ErrorMessage());return;}pRentRecordset->Close();pRentRecordset=NULL;//删除管理员部分代码:strSQLman.Format("delete from  management where  M_Account='%s' ",delID);strSQLtea.Format("delete from teacher where T_MgmName='%s' ",delID);try{//ADO事务开始m_pConnection->BeginTrans();hr = m_pConnection->Execute(_bstr_t(strSQLman),&RecordsAffected,adCmdText);hr = m_pConnection->Execute(_bstr_t(strSQLtea),&RecordsAffected,adCmdText);//ADO执行事务m_pConnection->CommitTrans();}catch (_com_error  *e){//ADO事务回滚m_pConnection->RollbackTrans();AfxMessageBox(e->ErrorMessage());return ;}//随即抽取的编号(iter)在数据库中查找记录并写入.txt文件生成试卷:hr =pDanXRecordset->Open("select * from SinTable",m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);while (!pDanXRecordset->adoEOF){if (curItem == *iter){var = pDanXRecordset->GetCollect("S_Content");if (var.vt!=VT_NULL){strValue = (LPCSTR)_bstr_t(var);}//把后面的空格去掉strValue.TrimRight();//把题目写进文件pFile<< cont++;strValue="、"+strValue;pFile<< (LPCSTR)strValue;pFile<< '/n';var = pDanXRecordset->GetCollect("S_Answer");if (var.vt!=VT_NULL){strValue = (LPCSTR)_bstr_t(var);}//把后面的空格去掉strValue.TrimRight();//把答案写进文件pFile<< "答案为:";pFile<< (LPCSTR)strValue;pFile<< '/n';if (iter != iSaveVect.end()){iter++;}else {break;}}curItem++;pDanXRecordset->MoveNext();}//在所有的简答题中随即选5道,(STL)std::vectoriVect;std::vectoriSaveVect;std::vector::iteratoriter;srand((unsigned)time(NULL));for (int i=1; i<=pJianDRecordset->RecordCount; i++){iVect.push_back(i);}for (i=0; i<2; i++){int k = rand() % iVect.size();iSaveVect.push_back( iVect.at(k) );iter = iVect.begin();for ( int j=0; j::iterator iterFirst = iSaveVect.begin();std::vector::iterator iterLast = iSaveVect.end();std::sort(iterFirst,iterLast);//排序iter = iSaveVect.begin();//写入文件_variant_tvar;CString strValue;CString FileName = GetDocument()->sSjName+".txt";int curItem = 1;int cout = 1;std::ofstream  pFile(FileName,std::ios::app);pFile<< "五、简答题(15*2)/n";while (!pJianDRecordset->adoEOF){if (curItem == *iter){var = pJianDRecordset->GetCollect("S_Content");if (var.vt != VT_NULL){strValue = (LPCSTR)_bstr_t(var);}//把后面的空格去掉strValue.TrimRight();//把题目写进文件pFile<< cout++;strValue = "、" + strValue;pFile<< (LPCSTR)strValue;pFile<< '/n';var = pJianDRecordset->GetCollect("S_Answer");if (var.vt != VT_NULL){strValue = (LPCSTR)_bstr_t(var);}//把后面的空格去掉strValue.TrimRight();//把答案写进文件pFile<< "答案为:";pFile<< (LPCSTR)strValue;pFile<< '/n';if (iter != iSaveVect.end()){iter++;}else {break;}}curItem++;pJianDRecordset->MoveNext();}pFile.close();