S
shilixiao
Unregistered / Unconfirmed
GUEST, unregistred user!
请高手们指点指点小弟,先谢谢啦!程序代码如下:
//备份数据库
procedure TBackup.Button1Click(Sender: TObject);
var
BackupFile : string;
DB : string;
begin
DB := 'JYKJ_db';
if SaveDialog.Execute then
if trim(SaveDialog.FileName) <> '' then
if DM.ADOConnDB.Connected = true then begin
DM.ADOConnDB.Connected := false;
BackupFile := SaveDialog.FileName + '.bak';
try
DM.ADOCommBak.CommandText := 'use Master';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'execute sp_helpdevice';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'backup database ' + DB + ' to disk=''' +BackupFile+ ''' with init';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'Use ' + DB ;
DM.ADOCommBak.Execute;
MessageBox(getactivewindow(),'成功备份数据库!','提示!' ,MB_OK + MB_ICONINFORMATION);
if DM.ADOConnDB.Connected = false then
DM.ADOConnDB.Connected := true;
except
MessageBox(getactivewindow(),'备份数据库失败!','错误!' ,MB_OK + MB_ICONERROR);
end;
end;
end;
//还原数据库
procedure TBackup.Button3Click(Sender: TObject);
var
RestoreFile : string;
DB : string;
begin
DB := 'JYKJ_db';
if OpenDialog.Execute then
if trim(OpenDialog.FileName) <> '' then
if DM.ADOConnDB.Connected = true then begin
DM.ADOConnDB.Connected := false;
RestoreFile := OpenDialog.FileName;
try
DM.ADOCommBak.CommandText := 'use Master';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'restore database ' + DB + ' from disk=''' +RestoreFile+ ''' with replace';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'use ' + DB ;
DM.ADOCommBak.Execute;
MessageBox(getactivewindow(),'成功恢复备份!','提示!' ,MB_OK + MB_ICONINFORMATION);
if MessageBox(GetActiveWindow(),'恢复成功后必须退出系统!','信息!',MB_OKCANCEL + MB_ICONINFORMATION) = IDOK then
Application.Terminate//还原成功后则退出系统
else
if DM.ADOConnDB.Connected = false then
DM.ADOConnDB.Connected := true;
except
MessageBox(getactivewindow(),'恢复备份失败!','错误!' ,MB_OK + MB_ICONERROR);
end;
end;
end;
//备份数据库
procedure TBackup.Button1Click(Sender: TObject);
var
BackupFile : string;
DB : string;
begin
DB := 'JYKJ_db';
if SaveDialog.Execute then
if trim(SaveDialog.FileName) <> '' then
if DM.ADOConnDB.Connected = true then begin
DM.ADOConnDB.Connected := false;
BackupFile := SaveDialog.FileName + '.bak';
try
DM.ADOCommBak.CommandText := 'use Master';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'execute sp_helpdevice';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'backup database ' + DB + ' to disk=''' +BackupFile+ ''' with init';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'Use ' + DB ;
DM.ADOCommBak.Execute;
MessageBox(getactivewindow(),'成功备份数据库!','提示!' ,MB_OK + MB_ICONINFORMATION);
if DM.ADOConnDB.Connected = false then
DM.ADOConnDB.Connected := true;
except
MessageBox(getactivewindow(),'备份数据库失败!','错误!' ,MB_OK + MB_ICONERROR);
end;
end;
end;
//还原数据库
procedure TBackup.Button3Click(Sender: TObject);
var
RestoreFile : string;
DB : string;
begin
DB := 'JYKJ_db';
if OpenDialog.Execute then
if trim(OpenDialog.FileName) <> '' then
if DM.ADOConnDB.Connected = true then begin
DM.ADOConnDB.Connected := false;
RestoreFile := OpenDialog.FileName;
try
DM.ADOCommBak.CommandText := 'use Master';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'restore database ' + DB + ' from disk=''' +RestoreFile+ ''' with replace';
DM.ADOCommBak.Execute;
DM.ADOCommBak.CommandText := 'use ' + DB ;
DM.ADOCommBak.Execute;
MessageBox(getactivewindow(),'成功恢复备份!','提示!' ,MB_OK + MB_ICONINFORMATION);
if MessageBox(GetActiveWindow(),'恢复成功后必须退出系统!','信息!',MB_OKCANCEL + MB_ICONINFORMATION) = IDOK then
Application.Terminate//还原成功后则退出系统
else
if DM.ADOConnDB.Connected = false then
DM.ADOConnDB.Connected := true;
except
MessageBox(getactivewindow(),'恢复备份失败!','错误!' ,MB_OK + MB_ICONERROR);
end;
end;
end;