你用ADOQuery,配置好连接字符串以后这样...with ADOQuery do begin if active then close; SQL.clear; SQL.add(备份语句);//你想备份哪个数据库就把哪个的数据库名字写进去 try ExecSQL; showmessage('备份成功!'); Except showmessage('有异常!'); end; end;
//备份数据库 procedure TBackupRestoreForm.BackupBtnClick(Sender: TObject); begin if Edit1.Text='' then begin showmessage('未选择目标文件名!'); exit end; try SQLQu_BackUp.SQL.Text:='BACKUP DATABASE [memdb] TO DISK = ''' + Edit1.Text + ''' WITH INIT'; SQLQu_BackUp.ExecSQL(true); ShowMessage('数据库备份完毕!'); except ShowMessage('数据库备份失败,请重新备份!'); end; end; //还原数据库 procedure TBackupRestoreForm.RestoreBtnClick(Sender: TObject); var BackupRestoreForm: TBackupRestoreForm; begin if Edit2.Text='' then begin ShowMessage('未选择源文件名!'); exit end; try try SQLQu_BackUp.SQL.Text:='use master'; SQLQu_BackUp.ExecSQL(true); SQLQu_BackUp.SQL.Text:='RESTORE DATABASE memdb from DISK =''' + Edit2.Text + '''' +' WITH FILE = 1, NOUNLOAD , STATS = 10, RECOVERY,replace'; SQLQu_BackUp.ExecSQL(true); finally BackupRestoreForm.Visible := true; end; ShowMessage('数据库恢复完毕!'); except ShowMessage('数据库恢复失败,请检查所选恢复文件是否正确。'+#13+'关闭其它应用程序,必要时重新启动计算机,然后重新恢复!'); end; end;
if Application.MessageBox(pchar('数据还原后需要重新启动程序,是否开始还原?'),pchar('询问'),mb_YesNo or Mb_IconQuestion) =IDYEs then begin AdoConnection1.Close; try AdoQuery1.Close; AdoQuery1.SQL.Clear; AdoQuery1.SQL.Add('use Master'); Adoquery1.ExecSQL; Adoquery1.SQL.Clear; Adoquery1.SQL.Add('Drop DataBase Student'); Adoquery1.ExecSQL; AdoQuery1.SQL.Clear; Adoquery1.SQL.Add('Create DataBase Student'); Adoquery1.ExecSQL; Adoquery1.SQL.Clear; Adoquery1.SQL.Add('Restore DataBase Student from disk = '''+CxTextEdit2.Text+''' with Replace '); Adoquery1.ExecSQL; Showmessage('数据还原成功,程序将重新启动!'); Application.Terminate; except On E:Exception do begin begin if Not AdoConnection1.InTransaction then AdoConnection1.RollbackTrans else begin showmessage('数据还原又异常,程序将重新启动!'); Application.Terminate; end; end; end; end;end;
lovepig888是我 那个程序数据库根本上部不掉 为什么啊
错误为No Transaction Active怎么回事了
Adoquery1.SQL.Add('Restore DataBase Student from disk = '''+CxTextEdit2.Text+''' with Replace '); //为什么要加with replace??什么意义你知道吗?我从来没加过with //replace
还原语句:use master restore database XXX(数据库名称) from disk='XXXXXXXXX'(存放备份文件的路径)//必须先use master,否则出错。
if active then close;
SQL.clear;
SQL.add(备份语句);//你想备份哪个数据库就把哪个的数据库名字写进去
try
ExecSQL;
showmessage('备份成功!');
Except
showmessage('有异常!');
end;
end;
procedure TBackupRestoreForm.BackupBtnClick(Sender: TObject);
begin
if Edit1.Text='' then
begin
showmessage('未选择目标文件名!');
exit
end;
try
SQLQu_BackUp.SQL.Text:='BACKUP DATABASE [memdb] TO DISK = ''' + Edit1.Text + ''' WITH INIT';
SQLQu_BackUp.ExecSQL(true);
ShowMessage('数据库备份完毕!');
except
ShowMessage('数据库备份失败,请重新备份!');
end;
end;
//还原数据库
procedure TBackupRestoreForm.RestoreBtnClick(Sender: TObject);
var
BackupRestoreForm: TBackupRestoreForm;
begin
if Edit2.Text='' then
begin
ShowMessage('未选择源文件名!');
exit
end;
try
try
SQLQu_BackUp.SQL.Text:='use master';
SQLQu_BackUp.ExecSQL(true);
SQLQu_BackUp.SQL.Text:='RESTORE DATABASE memdb from DISK =''' + Edit2.Text + ''''
+' WITH FILE = 1, NOUNLOAD , STATS = 10, RECOVERY,replace';
SQLQu_BackUp.ExecSQL(true);
finally
BackupRestoreForm.Visible := true;
end;
ShowMessage('数据库恢复完毕!');
except
ShowMessage('数据库恢复失败,请检查所选恢复文件是否正确。'+#13+'关闭其它应用程序,必要时重新启动计算机,然后重新恢复!');
end;
end;
begin
AdoConnection1.Close;
try
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('use Master');
Adoquery1.ExecSQL;
Adoquery1.SQL.Clear;
Adoquery1.SQL.Add('Drop DataBase Student');
Adoquery1.ExecSQL;
AdoQuery1.SQL.Clear;
Adoquery1.SQL.Add('Create DataBase Student');
Adoquery1.ExecSQL;
Adoquery1.SQL.Clear;
Adoquery1.SQL.Add('Restore DataBase Student from disk = '''+CxTextEdit2.Text+''' with Replace ');
Adoquery1.ExecSQL;
Showmessage('数据还原成功,程序将重新启动!');
Application.Terminate;
except
On E:Exception do
begin
begin
if Not AdoConnection1.InTransaction then
AdoConnection1.RollbackTrans
else
begin
showmessage('数据还原又异常,程序将重新启动!');
Application.Terminate;
end;
end;
end;
end;end;
那个程序数据库根本上部不掉
为什么啊
//为什么要加with replace??什么意义你知道吗?我从来没加过with
//replace