加载内存流时访问冲突

时间:2015-03-11 17:55:15

标签: c++ datasnap tstream

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

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

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;
}

所以我想知道为什么这段代码有效,但只在第一次迭代时使用while。查询的结果是正确的,代码加载和保存正常,但仅针对que查询的第一个结果。为此工作的解决方法将是很好的。

0 个答案:

没有答案