我有一个在 Delphi 6 中制作的 Windows 服务,它连接到一个进度 opensge 数据库,并每天一次将信息传输到 MySQL 数据库。问题是,当我启动服务时,它工作正常,但如果我更新进度数据库,第二天服务仍然显示第一个值(相同的值)。就像我有一个数据库图像并且它没有得到更新。如果我重新启动服务,他们将获得新的值,但在第二天会发生同样的事情。
对于数据库连接,我使用 TDatabase 类。任何人都知道如何解决这个问题?
这是代码:
unit svcMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ExtCtrls, AppEvnts, DateUtils, inifiles;
type
TsvcAgraria = class(TService)
timAcao: TTimer;
procedure timAcaoTimer(Sender: TObject);
procedure ServicePause(Sender: TService; var Paused: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceContinue(Sender: TService; var Continued: Boolean);
function RightPad(S: string; Ch: Char; Len: Integer): string;
private
{ Private declarations }
procedure Gera_log(ctipo,msgtxt:string);
procedure Replica_Dados();
procedure Verifica_Ini();
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
svcAgraria: TsvcAgraria;
lsup1,lsup2,lsupexc,ltudook:boolean;
cont_registros:integer;
teste,loteini,lotefim,tLooping:string;
implementation
uses UdtmRamal;
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
svcAgraria.Controller(CtrlCode);
end;
function TsvcAgraria.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TsvcAgraria.ServiceStart(Sender: TService; var Started: Boolean);
begin
Gera_Log('INFO','Iniciado o Serviço Windows para Replicação de Ramais.');
timAcao.Enabled := Started;
end;
procedure TsvcAgraria.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
timAcao.Enabled := not(Stopped);
Gera_Log('INFO','Finalizado o Serviço Windows para Replicação de Ramais.');
end;
procedure TsvcAgraria.ServicePause(Sender: TService; var Paused: Boolean);
begin
timAcao.Enabled := not(Paused);
Gera_Log('INFO','Serviço Pausado.');
end;
procedure TsvcAgraria.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
timAcao.Enabled := Continued;
Gera_Log('INFO','Serviço em Execução.');
end;
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
procedure TsvcAgraria.timAcaoTimer(Sender: TObject);
var tagora:string;
begin
Verifica_Ini();
tagora := RightPad(IntToStr(HourOf(Now)), '0', 2) + ':' + RightPad(IntToStr(MinuteOf(Now)), '0', 2);
//Gera_Log('INFO','Valores à comparar: var "' + tagora + '", ini "' + tLooping + '".');
if (tLooping = tagora) then
begin
Replica_Dados();
end;
end;
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
procedure TsvcAgraria.Replica_Dados();
var
_validaprof:integer;
begin
Gera_Log('INFO','--- Iniciando a Replicação de Dados para a Intranet...');
/////////////////////////////////////////////////
try
dtmAtualizaRamal.DbMyRamal.Connected:=true;
dtmAtualizaRamal.ConectDBHR.Connected:=true;
if (dtmAtualizaRamal.ConectDBHR.Connected and dtmAtualizaRamal.DbMyRamal.Connected) then
begin
/* BUSINESS LOGIC */
end;
except
on E: Exception do Gera_Log('ERRO',E.Message);
end;
Gera_Log('INFO','--- Desconectar Banco.');
dtmAtualizaRamal.DbMyRamal.Connected:=false;
dtmAtualizaRamal.ConectDBHR.Connected:=false;
Gera_Log('INFO','--- Final da Replicação de Dados para a Intranet.');
end;
function TsvcAgraria.RightPad(S: string; Ch: Char; Len: Integer): string;
var
RestLen: Integer;
begin
Result := S;
RestLen := Len - Length(s);
if RestLen < 1 then Exit;
Result := StringOfChar(Ch, RestLen) + S;
end;
procedure TsvcAgraria.Gera_log(ctipo,msgtxt:string);
var
LogFileName,LogData:string;
LogFile:TextFile;
begin
LogFileName := 'c:\datasul\temp\scvRamais.log.txt';
AssignFile(LogFile, LogFileName);
//either create an error log file, or append to an existing one
if FileExists(LogFileName) then
Append(LogFile)
else
Rewrite(LogFile);
try
//add the current date/time and the exception message to the log
LogData := Format('%s : %s : %s',[DateTimeToStr(Now),ctipo,msgtxt]) ;
WriteLn(LogFile,LogData) ;
finally
CloseFile(LogFile);
end;
end;
end.