我通过recordset.open从数据库中得到1000条记录,然后通过MOVENEXT循环读取每条记录,开始的时候很快,当读到300条后开始变慢,而且越来越慢,实在不知道是什么原因,哪位高手帮帮我

解决方案 »

  1.   

    检测是不是 内存没释放没循环一条记录,就把当前的内存释放调(比如rsrecord)
      

  2.   

    代码如下:
    for(;1;)
    {
    /////////////////////////////连接数据库//////////////////////////////////////
      csConfig.Lock();
      CString strCon = "Provider=SQLOLEDB; Server=";
      strCon = strCon + g_config.DBServer;
      strCon = strCon + "; Database=";
      strCon = strCon + g_config.Database;
      strCon = strCon + "; uid=";
      strCon = strCon + g_config.DBUser;
      strCon = strCon + "; pwd=";
      strCon = strCon + g_config.DBPwd;
      csConfig.Unlock();
      try
      {
    pCon1 = NULL;
    HRESULT hr = pCon1.CreateInstance(__uuidof(Connection));
    if(FAILED(hr))
    {
       ::MessageBox(NULL,"连接数据库失败","连接数据库失败",MB_OK |MB_ICONWARNING);
       return TRUE;
             }
    pCon1->Open(strCon.AllocSysString(),"", "", adConnectUnspecified);
       }
       catch (_com_error &e) 
       {
         ::MessageBox(NULL,e.Description(),"连接数据库失败",MB_OK | MB_ICONWARNING);
       }    _CommandPtr pCmd = NULL;
       _ParameterPtr param = NULL;
       _RecordsetPtr pRec1 = NULL;   pCmd.CreateInstance(__uuidof(Command));
       pCmd->ActiveConnection = pCon1;
       pCmd->CommandText = _bstr_t("get_MTQueue");
       pCmd->CommandType = adCmdStoredProc;   param = pCmd->CreateParameter(_bstr_t("CURRENT_ID"), adInteger, adParamInput, 4, _variant_t((long)nRecordID)); 
       pCmd->Parameters->Append(param);
       pRec1.CreateInstance(__uuidof(Recordset));
       pRec1->CursorLocation=adUseClient;
       pRec1->PutRefSource((IDispatch *)pCmd);
       pRec1->CacheSize = 1000;
       
       try
      {     pCmd->Parameters->Item[_bstr_t("CURRENT_ID")]->Value = _variant_t((long)nRecordID);
         pRec1->Open(vtMissing, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnspecified);     long nRecCount =  pRec1->RecordCount;
         if(nRecCount == 0) //如果数据库表MTQUEUE中没有数据
         {
    pRec1->Close();
    Sleep(time * 1000);
    continue;
         }
         pRec1->MoveFirst();
         for(int ss = 0; ss < nRecCount; ss++)
         {
    A = (long)pRec1->GetCollect("A");
    B = (char*)(_bstr_t)(pRec1->GetCollect("B"));
    C =(short)(pRec1->GetCollect("C"));
    D = (char*)(_bstr_t)(pRec1->GetCollect("D"));
    E = (char*)(_bstr_t)(pRec1->GetCollect("E"));
    F = (char*)(_bstr_t)(pRec1->GetCollect("F"));
    G = (char*)(_bstr_t)(pRec1->GetCollect("G"));
    H = (char*)(_bstr_t)(pRec1->GetCollect("H"));
    I = (short)(pRec1->GetCollect("I"));
    J =(char*)(_bstr_t)(pRec1->GetCollect("J"));
    pRec1->MoveNext();

          }
          pRec1->Close();
          pCon1->Close();
    }
    这段代码是在一段定时循环的无限循环里面,循环开始连接数据库,然后执行一个存储过程,读取RECORDSET里面的记录,当第一次执行这个存储过程完成的时候读取RECORDSET(100条记录),速度很快,当第二次循环执行完存储过程读取RECORDSET时,明显变慢,第三次就慢的无法忍受。我在代码里面用完后把pRec1和pCon1都关闭了,应该不是内存泄漏的问题,大侠们帮我看看了
      

  3.   

    慢的地方就是在
    A = (long)pRec1->GetCollect("A");
    B = (char*)(_bstr_t)(pRec1->GetCollect("B"));
    C =(short)(pRec1->GetCollect("C"));
    D = (char*)(_bstr_t)(pRec1->GetCollect("D"));
    E = (char*)(_bstr_t)(pRec1->GetCollect("E"));
    F = (char*)(_bstr_t)(pRec1->GetCollect("F"));
    G = (char*)(_bstr_t)(pRec1->GetCollect("G"));
    H = (char*)(_bstr_t)(pRec1->GetCollect("H"));
    I = (short)(pRec1->GetCollect("I"));
    J =(char*)(_bstr_t)(pRec1->GetCollect("J"));
    这几段读取RECORDSET的代码里面里面,其他的都很快,包括pRec1->Open(vtMissing, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnspecified)这段代码也都很快
      

  4.   

    写成这个样子试一试
    B = (char*)(_bstr_t)(pRec1->Fields->GetItem("B")->Value);
      

  5.   

    试一下Server端游标,和仅向前的游标。