Please help, I don't know what I am doing wrong.
I am developing an application that retrieves files from a server machine, for autoupdate the client machines. this is what I came up with:
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;
}
So I want to know why this code works, but only on first iteration with the while. The results of the query are right, and the code loads and saves alright, but only for the first result of que query. And a workaround for this to work would be nice.