📄 waitpas1.pas
字号:
{数据备份、还原}
unit WaitPas1;
interface
uses
Classes, Windows, SysUtils, Dialogs, ADODB ;
type
WaitPas = class(TThread)
Filename : String; //往线程中注入备份或还原的文件名
Messages : String; //显示信息
IDs : Integer; //识别应该执行什么样的操作
ADOQ : TADOQuery;
AddData : Integer; //是否备份数据压缩 0=false 1=true
AddMode : Integer; //数据压缩方式<0..5>
//备份文件
function BackUpData:Boolean;
//还原文件
function RevertData:Boolean;
//关闭等待窗口
procedure CloseWaitWindow;
//更新等待窗口信息
procedure PostMessgesToWaitWindow;
//发送数据备份结果
procedure SendEnd;
private
{ Private declarations }
protected
procedure Execute; override;
public
IsSucceed : Boolean;
constructor Create(CaseS: TADOQuery; FileNames:String;LoadID,AddDatas,AddModes:integer);
end;
implementation
uses progress, MAIN, Option, BackUp;
{ WaitPas }
//等待程序运行结束
function WinExecAndWait32(FileName:String; Visibility : integer): DWORD;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin //程序运行函数
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(
nil,
zAppName, { 指向命令行字符串 }
nil, { 指向过程安全属性}
nil, { 线程安全属性 }
false, { 标记 }
CREATE_NEW_CONSOLE or { 创建标记 }
NORMAL_PRIORITY_CLASS,
nil,
nil,
StartupInfo,
ProcessInfo)
then begin
Result := 1;
end
else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
function WaitPas.BackUpData:Boolean;
var
ChangeS :String;
begin
Messages:='正在备份数据库,请稍候……';
Synchronize(PostMessgesToWaitWindow);
ChangeS:= ChangeFileExt(FileName,'.Zbk');
with ADOQ do try
Close;
SQL.Clear;
SQL.Add('backup database CRMS to disk='+#39+ChangeS+#39+' WITH INIT');
Prepared;
Execsql;
// 调用“RAR。EXE”执行压缩
if AddData=1 then begin
//改变系统当前目录
SetCurrentDir(ExtractFilePath(paramstr(0))+'BackUp\');
WinExecAndWait32(Pchar(ExtractFilePath(paramstr(0))+'Rar.exe m -m'+IntToStr(AddMode)+' '+FileName+' '+ExtractFileName(ChangeS)),SW_HIDE);
//改回系统当前目录
SetCurrentDir(ExtractFilePath(paramstr(0)));
end;
Result:=true;
except
Result:=false;
end;
end;
procedure WaitPas.CloseWaitWindow;
begin
WaitForm.Close;
end;
constructor WaitPas.Create(CaseS: TADOQuery; FileNames: String; LoadID,AddDatas,AddModes: Integer);
begin
//初始化进程
IDs := LoadID;
ADOQ := CaseS;
AddData := AddDatas;
AddMode := AddModes;
FileName := FileNames;
inherited Create(False);
end;
procedure WaitPas.Execute;
begin
IsSucceed:=false;
case IDs of
1 : IsSucceed := BackUpData; //备份
2 : IsSucceed := RevertData; //还原
end;
//关闭等待窗口
Synchronize(CloseWaitWindow);
//提示操作结果
Synchronize(SendEnd);
{ Place thread code here }
end;
procedure WaitPas.PostMessgesToWaitWindow;
begin
WaitForm.Label1.Caption:=Messages;
end;
function WaitPas.RevertData: Boolean;
var
S : String;
begin
Messages:='正在还原数据库,请稍候……';
Synchronize(PostMessgesToWaitWindow);
//调用“RAR。EXE”执行解压过程
if AddData=1 then
WinExecAndWait32(Pchar('Rar.exe e '+FileName),SW_HIDE);
S:= ChangeFileExt(FileName,'.Zbk');
with ADOQ do try
Close;
SQL.Clear;
sql.Add('use master');
sql.Add('ALTER DATABASE CRMS SET OFFLINE WITH ROLLBACK IMMEDIATE');
sql.Add('RESTORE DATABASE CRMS FROM DISK ='''+S+''' with FILE=1,RECOVERY,REPLACE');
sql.Add('ALTER DATABASE CRMS SET ONLINE WITH ROLLBACK IMMEDIATE');
SQL.Add('Use CRMS');
Prepared;
execsql;
if AddData=1 then //如果是压缩文件,则还原后删除解压缩文件
DeleteFile(S);
Result:=true;
except
Result:=false;
end;
end;
procedure WaitPas.SendEnd;
begin
//发送数据备份结果
BackUpForm.ShowProcessMessages(IsSucceed);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -