📄 restorethreadunit.pas
字号:
//*-----------------------------------------------------------------------------
//* 系统名称:数据自动备份系统
//* 作 者:陈春华
//* 开发时间:2005
//* Windows2000 Server + SQL Server2000 + Delphi7
//* 功能简介:
//* 软件可自动完成SQL Server数据库和文档的数据定时自动备份,
//* 由用户选择对哪些数据库或文档进行数据备份,提供对备份、还原
//* 数据作业的管理、分析、日志跟踪等功能,备份频率不受限制。
//*-----------------------------------------------------------------------------
unit RestoreThreadUnit;
interface
uses
Classes, DB, ADODB, SysUtils;
type
TRestoreThread = class(TThread)
private
{ Private declarations }
DBName: String; //数据库名
DBPhysicalAdd: String; //数据库物理位置
restoreFile: String; //还原源数据
conn:TADOConnection;
procedure restoreDone;
protected
procedure Execute; override;
public
constructor Create(_DBName,_DBPhysicalAdd,_restoreFile:String; _conn:TADOConnection);
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TRestoreThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TRestoreThread }
uses SqlFunctionUnit, DBRestoreUnit, DataModuleUnit;
constructor TRestoreThread.Create(_DBName,_DBPhysicalAdd,_restoreFile:String;_conn:TADOConnection);
begin
DBName:=_DBName;
DBPhysicalAdd:=_DBPhysicalAdd;
restoreFile:=_restoreFile;
conn:=_conn;
inherited Create(True);
end;
procedure TRestoreThread.restoreDone;
var
rs,rss:TADOQuery;
isWrong:boolean;
beginTime, endTime: TDateTime;
LogFile: text;
begin
isWrong:=False;
beginTime:=now;
try
try
rs:= TADOQuery.Create(nil);
rs.Connection:=conn;
rss:= TADOQuery.Create(nil);
rss.Connection:=conn;
with rs do
begin
close;
SQL.Clear;
SQL.Add('RESTORE FILELISTONLY FROM DISK ='''+restoreFile+'''');
Open;
with rss do
begin
Close;
SQL.Clear;
SQl.Add('RESTORE DATABASE '+'"'+DBName+'"'+' FROM DISK = '''+restoreFile+'''');
SQl.Add('WITH Replace, MOVE '+''''+rs.fieldbyname('logicalname').AsString+''''+
' TO '+''''+ExtractFilePath(DBPhysicalAdd)+ExtractFileName(rs.fieldbyname('physicalname').AsString)+'''');
rs.next;
SQL.Add(', Move '+''''+rs.fieldbyname('logicalname').AsString+''''+
' TO '+''''+ExtractFilePath(DBPhysicalAdd)+ExtractFileName(rs.fieldbyname('physicalname').AsString)+'''');
ExecSQL;
end;
end;
except
isWrong:=True;
end;
finally
rs.Close;
rs.Free;
rss.close;
rss.Free;
AssignFile(LogFile,ExtractFilePath(ParamStr(0))+'\Log\DBBackupLog.log');
if fileExists(ExtractFilePath(ParamStr(0))+'\Log\DBBackupLog.log') then
Append(LogFile)
else
Rewrite(LogFile);
if isWrong then //报错处理
begin
writeln(LogFile,DateTimeToStr(beginTime)+' 数据库'+DBName+'还原失败。');
RestoreForm.Label6.Caption:='数据库'+DBName+'还原失败!';
end
else
begin
writeln(LogFile,DateTimeToStr(beginTime)+' 数据库'+DBName+'还原成功。耗时:'+TimeToStr(endTime-beginTime));
RestoreForm.Label6.Caption:='数据库'+DBName+'还原成功!';
end;
CloseFile(LogFile);
end;
end;
procedure TRestoreThread.Execute;
begin
{ Place thread code here }
FreeOnTerminate:=True;
synchronize(restoreDone);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -