//这是全部的代码,这个服务的主要目的是:
每天零时执行一次删除数据库记录和FTP目录下对应的物理文件
每小时(timer设置的是一小时)判断当前时间是不是零时。
1。EXE()是我直接从应用程序里搬出来的。
2。错误发生在[red]红色[/red]标记块(在应用程序中正确)
unit DeleteService;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, SvcMgr,Psock, DB, ADODB,Dialogs,
ExtCtrls,OleServer, YWQftpclass_TLB, Parameter_TLB;//两个DLL
type
Tmain = class(TService)
ADOConnection1: TADOConnection;
Timer1: TTimer;
cFtpApp1: TcFtpApp;
RegDataConn1: TRegDataConn;
ADOQuery1: TADOQuery;
procedure Timer1Timer(Sender: TObject);
procedure ServiceContinue(Sender: TService;
var Continued: Boolean);
procedure ServicePause(Sender: TService;
var Paused: Boolean);
procedure ServiceStart(Sender: TService;
var Started: Boolean);
procedure ServiceStop(Sender: TService;
var Stopped: Boolean);
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
//=======公共自定义部分
function GetFileSize(filename: String): Integer;
//取得文件大小
function WriteLogFile(StrLog: String): Boolean;
//把字符串写入日志文件中
//=======资源库
procedure ResourceLib();
procedure DeleteRecordes(ID:string);
procedure DeleteResourceFile(ComplexFlag,ResourcePath:widestring);
public
function GetServiceController: TServiceController;
override;
procedure Exe();
//执行流程
{ Public declarations }
end;
var
main: Tmain;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
main.Controller(CtrlCode);
end;
function Tmain.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure Tmain.Exe();//执行流程
begin
ResourceLib();
//showmessage('ResourceLib');
end;
//------------------------------------------------------------------------------
procedure Tmain.Timer1Timer(Sender: TObject);
var
Present: TDateTime;
Hour, Min, Sec, MSec: Word;
begin
Present:= Now;
DecodeTime(Present, Hour, Min, Sec, MSec);
//showmessage('start!');
WriteLogFile(DateTimeToStr(Now)+':执行开始');
//showmessage('执行开始')
if (Hour=0) then
//零点执行
begin
Exe();
end;
end;
//------------------------------------------------------------------------------
function Tmain.GetFileSize(filename: String): Integer;
var
F: file of Byte;
begin
try
AssignFile(F,filename);
Reset(F);
Result := FileSize(F);
CloseFile(F);
except
CloseFile(F);
Result := 0;
end;
end;
//------------------------------------------------------------------------------
function Tmain.WriteLogFile(StrLog: String): Boolean;
//把字符串写入日志文件中
var
CurrentPath: String;
v_result: Boolean;
LogF: TextFile;
//日志文件
begin
v_result := True;
try
if [red]RegDataConn1.GetRegData [/red]then
CurrentPath:= RegDataConn1.SoftwarePath
//else
// showmessage(RegDataConn1.Error);
if CurrentPath='' then
begin
Result:= False;
Exit;
end
else
;
if CurrentPath[Length(CurrentPath)] <> '/' then
CurrentPath := CurrentPath + '/';
if not DirectoryExists(CurrentPath+'App') then
CreateDir(CurrentPath+'App');
CurrentPath := CurrentPath + 'App/';
if not DirectoryExists(CurrentPath+'Log') then
CreateDir(CurrentPath+'Log');
try
if FileExists(CurrentPath+'Log/DeleteService.txt') then
if GetFileSize(CurrentPath+'Log/DeleteService.txt') > 1*1024 then
//判断日志文件是否大于1M
RenameFile(CurrentPath+'Log/DeleteService.txt',CurrentPath+'Log/DeleteService'+DateTimeToStr(Now)+'.log');
except
On E:Exceptiondo
ShowMessage(E.Message);
end;
AssignFile(LogF,CurrentPath+'Log/DeleteService.txt');
//查找是否存在文件DeleteService.txt
if FileSearch('DeleteService.txt',CurrentPath+'Log') = '' then
begin
//文件不存在
Rewrite(LogF);
end
else
begin
//文件存在
Append(LogF);
end;
Writeln(LogF,StrLog);
Flush(LogF);
except
v_result := False;
if (@LogF <> nil) then
CloseFile(LogF);
end;
Result := v_result;
end;
//------------------------------------------------------------------------------
// 删除资源表中已经标记为删除的资源记录和物理文件
//
//------------------------------------------------------------------------------
procedure Tmain.ResourceLib();
var
DSN,UID,PWD: widestring;
ConStr,SQLStr: string;
rcd: _recordset;
vStr1,vStr2,vStr3,vStr4:widestring;
begin
SQLStr:='select ResourceTable.ResourceID, '
+ 'ResourceTable.URIType, '
+ 'Rtrim(LTRIM(DiskTable.VirtualPath))+ Rtrim(LTRIM(ResourceTable.[Identifier])) as Path,'
+ 'ResourceTable.ComplexFlag '
+'from ResourceTable,Disktable '
+'where (ResourceTable.VirtualPathID=DiskTable.VirtualPathID) and '
+ 'ResourceTable.ResourceID in '
+ '(select ResourceID from ResourceReferenceView where Counter=0 ) ';
if [red]RegDataConn1.GetRegData[/red] then
//读注册表参数
begin
DSN:=RegDataConn1.ODBCDSN ;
//======ODBC数据库配置参数
UID:=RegDataConn1.ODBCUID;
PWD:=RegDataConn1.ODBCPWD;
ConStr:='Provider=MSDASQL.1;Password='
+PWD+';Persist Security Info=False;User ID='
+UID+';Data Source='
+DSN ;
end
else
begin
WriteLogFile('注册表读取失败:时间='+DateTimeToStr(Now)+'信息:ODBC参数不能获得。'+trim(RegDataConn1.Error));
exit;
end;
}
ConStr:='Provider=MSDASQL.1;Password='
+'love'+';Persist Security Info=False;User ID='
+'sa'+';Data Source=ChinaSchool_RS40';
ADOConnection1.ConnectionString:=ConStr;
try
[red]ADOConnection1.Open[/red];
except
On E:Exceptiondo
ShowMessage(E.Message);
ADOConnection1.Close;
WriteLogFile('数据库连接失败:时间='+DateTimeToStr(Now)+'信息:连接串='+ConStr);
exit;
end;
showmessage('ADOConnection1.Open;');
try
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(SQLStr);
ADOQuery1.Open;
rcd:=ADOQuery1.Recordset;
ADOQuery1.Close;
except
On E:Exceptiondo
ShowMessage(E.Message);
ADOQuery1.Close;
WriteLogFile('数据库查询失败:时间='+DateTimeToStr(Now)+'信息:查询SQL='+SQLStr);
exit;
end;
if rcd.RecordCount <>0 then
begin
rcd.MoveFirst;
while not rcd.EOFdo
begin
//------------------------
vStr1:=rcd.Fields[0].Value;
// 资源ID
vStr2:=rcd.Fields[1].Value;
// 资源类型
vStr3:=rcd.Fields[2].Value;
// 路径
vStr4:=rcd.Fields[3].Value;
//复合标记
//------------------------
DeleteRecordes(vStr1);
if vStr2='L' then
DeleteResourceFile(vStr4,vStr3);
rcd.MoveNext;
end;
end;
end;
//------------------------------------------------------------------------------
// 删除记录
// ID-> 资源表的ResourceID
//------------------------------------------------------------------------------
procedure Tmain.DeleteRecordes(ID:string);
var
SQlStr:string;
begin
SQlStr:='DELETE FROM ResourceTable where ResourceID='+ID;
try
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(SQlStr);
ADOQuery1.ExecSQL;
ADOQuery1.Close;
WriteLogFile('记录删除成功:时间='+DateTimeToStr(Now)+'信息:资源ID='+ID);
//应该写入日志文件
except
ADOQuery1.Close;
WriteLogFile('记录删除失败:时间='+DateTimeToStr(Now)+'信息:资源ID='+ID);
//应该写入日志文件
exit;
end;
end;
//------------------------------------------------------------------------------
// 删除物理文件
// ComplexFlag -> 资源文件复合标记
// ResourcePath -> 资源的绝对路径
//------------------------------------------------------------------------------
procedure Tmain.DeleteResourceFile(ComplexFlag,ResourcePath:widestring);
begin
if [red]RegDataConn1.GetRegData[/red] then
begin
cFtpApp1.FtpServerName:=RegDataConn1.FTPServerIP;
cFtpApp1.FtpUserID:=RegDataConn1.FTPUser;
cFtpApp1.FtpPassword:=RegDataConn1.FTPPwd;
cFtpApp1.FtpPort:=RegDataConn1.FtpPort;
end
else
begin
WriteLogFile('注册表读取失败:时间='+DateTimeToStr(Now)+'信息:FTP参数不能获得。'+trim(RegDataConn1.Error));
//写入日志文件
exit;
end;
}
cFtpApp1.URIType:='L';
cFtpApp1.ComplexFlag:=ComplexFlag;
cFtpApp1.SourceRemoteMainURI:=ResourcePath;
if cFtpApp1.DeleteResoure then
//删除资源文件
WriteLogFile('资源文件删除成功:时间='+DateTimeToStr(Now)+'信息:资源路径='+ResourcePath)
//showmessage('资源文件删除成功:时间='+DateTimeToStr(Now)+'信息:资源路径='+ResourcePath) //写入日志文件
else
WriteLogFile('资源文件删除失败:时间='+DateTimeToStr(Now)+'信息:'+trim(cFtpApp1.GetErrString));
//写入日志文件
cFtpApp1.Disconnect;
end;
procedure Tmain.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
Timer1.Enabled :=True;
WriteLogFile('Service has been Continued at '+DateTimeToStr(Now));
Continued := True;
end;
procedure Tmain.ServicePause(Sender: TService;
var Paused: Boolean);
begin
Timer1.Enabled := False;
WriteLogFile('Service has been Paused at '+DateTimeToStr(Now));
Paused := True;
end;
procedure Tmain.ServiceStart(Sender: TService;
var Started: Boolean);
begin
Timer1.Interval:=1000;
showmessage('服务开始'+DateTimeToStr(Now));
WriteLogFile('Service has been Started at '+DateTimeToStr(Now));
Started:=True;
end;
procedure Tmain.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Timer1.Enabled := False;
WriteLogFile('Service has been Stopped at '+DateTimeToStr(Now));
Stopped:= True;
end;
procedure Tmain.ServiceExecute(Sender: TService);
begin
WriteLogFile('Service has been Started at '+DateTimeToStr(Now));
Timer1.Enabled := True;
//showmessage(' Timer1.Enabled := True;')
end;
end.