0

请帮忙,我不知道我做错了什么。

我正在开发一个从服务器机器检索文件的应用程序,用于自动更新客户端机器。这就是我想出的:

void TAutoUpdate::CarregarDados()
{
   try{
      TQuery* Q = new TQuery(NULL);
      Q->DatabaseName = "Base";
      Q->SQL->Add("SELECT * FROM UPDATE_MODULES WHERE DT_UPDATE > " + QuotedStr(ReadIni("ICDNetXP", "AutoUpdate", "DataUpdate", "12/30/1899")));
      Q->Prepare();
      Q->Active = true;
      Q->First();

      MemoryStream = new TMemoryStream;
      MemoryStream->Seek(0, soFromBeginning);

      SQLConnectionServer->Connected = true;
      TSMClient* Servidor = new TSMClient(SQLConnectionServer->DBXConnection);
      TDateTime maiorDataUpdate;
      String caminho;
      while(!Q->Eof){
         MemoryStream->Clear();

         if(maiorDataUpdate < Q->FieldByName("DT_UPDATE")->AsDateTime)
            maiorDataUpdate = Q->FieldByName("DT_UPDATE")->AsDateTime;

         caminho = "";
         caminho += Q->FieldByName("PATH")->AsString + Q->FieldByName("NOME")->AsString;
         MemoryStream->LoadFromStream(CopyStream(Servidor->GetAtualizacaoSistema(Q->FieldByName("ID")->AsString))); //here the error occours on 2nd execution of the while block
         MemoryStream->Seek(0, soFromBeginning);
         MemoryStream->SaveToFile(caminho);
         Q->Next();
      }
   }catch(Exception & e){
      MessageDlg(e.Message, mtError, TMsgDlgButtons() << mbOK, 0);
   }
}

TStream* TAutoUpdate::CopyStream(TStream* StreamIn)
{
      int BytesRead;
      System::PByte Buffer;
      const MaxBufSize = 1;
      TMemoryStream* StreamOut = new TMemoryStream;

      StreamIn->Seek(0, soFromBeginning);
      StreamOut->Seek(0, soFromBeginning);

      do{
         BytesRead = StreamIn->Read(Buffer, MaxBufSize);
         if(BytesRead > 0)
            StreamOut->Write(Buffer, MaxBufSize);

      }while(BytesRead > 0);

      StreamOut->Seek(0, soFromBeginning);
      StreamOut->SaveToFile("teste");
      return StreamOut;
}

所以我想知道为什么这段代码有效,但只在第一次迭代时才有效。查询结果是对的,代码加载和保存都正常,但只针对que查询的第一个结果。解决这个问题的方法会很好。

4

0 回答 0