function TfrmMain.DateOption(vInt: Integer): Boolean; var vSQL : String; DataPath : String; begin DataPath := GetDataPath(vInt); if DataPath <> '' then if vInt = 0 then begin vSQL := 'BACKUP DATABASE Bar TO DISK = ''' + ChangeFileExt(DataPath,'.BAK') + ''''; DataExecute(vSQL); end else begin dmMain.ADOConnection1.Connected := False; vSQL := 'ALTER DATABASE Bar SET OFFLINE WITH ROLLBACK IMMEDIATE'; DataExecute(vSQL); vSQL := 'RESTORE DATABASE Bar FROM DISK = ''' + DataPath + ''''; DataExecute(vSQL); vSQL := 'ALTER DATABASE Bar SET ONLINE WITH ROLLBACK IMMEDIATE'; DataExecute(vSQL); end; end;
其中: function TfrmMain.GetDataPath(vInt: Integer): String; var PathForm : TPathForm; begin Result := ''; Try PathForm := TPathForm.Create(Nil); if vInt = 0 then begin PathForm.Caption := '请选择数据库备份路径'; PathForm.Hint := 'Backup'; end else begin PathForm.Caption := '请选择数据库恢复路径'; PathForm.Hint := 'Restory'; end; PathForm.ModalResult := mrCancel; PathForm.ShowModal; Finally Result := PathForm.FPath; FreeAndNil(PathForm); end; end;function TfrmMain.DataExecute(vSQLStr: String): Boolean; begin with DMMain.ADOCommand1 do begin CommandText := vSQLStr; Execute; end; end;
'BACKUP DATABASE Bar TO DISK = ''' + ChangeFileExt(DataPath,'.BAK') + ''''
var
vSQL : String;
DataPath : String;
begin
DataPath := GetDataPath(vInt);
if DataPath <> '' then
if vInt = 0 then
begin
vSQL := 'BACKUP DATABASE Bar TO DISK = ''' + ChangeFileExt(DataPath,'.BAK') + '''';
DataExecute(vSQL);
end else
begin
dmMain.ADOConnection1.Connected := False;
vSQL := 'ALTER DATABASE Bar SET OFFLINE WITH ROLLBACK IMMEDIATE';
DataExecute(vSQL);
vSQL := 'RESTORE DATABASE Bar FROM DISK = ''' + DataPath + '''';
DataExecute(vSQL);
vSQL := 'ALTER DATABASE Bar SET ONLINE WITH ROLLBACK IMMEDIATE';
DataExecute(vSQL);
end;
end;
function TfrmMain.GetDataPath(vInt: Integer): String;
var
PathForm : TPathForm;
begin
Result := '';
Try
PathForm := TPathForm.Create(Nil);
if vInt = 0 then
begin
PathForm.Caption := '请选择数据库备份路径';
PathForm.Hint := 'Backup';
end else
begin
PathForm.Caption := '请选择数据库恢复路径';
PathForm.Hint := 'Restory';
end;
PathForm.ModalResult := mrCancel;
PathForm.ShowModal;
Finally
Result := PathForm.FPath;
FreeAndNil(PathForm);
end;
end;function TfrmMain.DataExecute(vSQLStr: String): Boolean;
begin
with DMMain.ADOCommand1 do
begin
CommandText := vSQLStr;
Execute;
end;
end;
我也是从网上搜的别人的,然后整理了一下,我做的东西里就是这么用的,可以实现!
你试一试!