CSDN博客

img hoge66

delphi中实现Sqlserver2000数据库的备份与还原!

发表于2004/7/6 9:52:00  2491人阅读

备份:
var
FileName:string;
strsql:string;

begin
if openDialog1.Execute then
  begin
  FileName:=openDialog1.FileName;
  showmessage(FileName);
  try
  strsql:='backup database carxt to disk='''+FileName+'''';
  rs.Close;
  rs.SQL.Clear;
  rs.SQL.Add(strsql);
  rs.ExecSQL;
  if fileExists(FileName)then
  showmessage('已经将carxt备份为:'+FileName);
  except
  messageDlg('数据备份异常!',MTError,[mbok],0);
  end;
  end;

还原:
if openDialog1.Execute then
begin
try
conn.Connected:=false;//关闭carxt数据库
conn2.Open;
rs.Connection:=conn2;
rs.Close;
rs.SQL.Clear;
rs.SQL.Add('ALTER DATABASE carxt01 SET OFFLINE WITH ROLLBACK IMMEDIATE');
rs.ExecSQL;
rs.Close;
rs.SQL.Clear;
rs.SQL.Add('restore database carxt01 From disk='''+openDialog1.FileName+''' ');
rs.ExecSQL;
showmessage('顺利完成数据库还原!');
finally
rs.Close;
rs.SQL.Clear;
rs.SQL.Add('ALTER DATABASE carxt01 SET ONLINE WITH ROLLBACK IMMEDIATE');//重新连接数据库carxt01
rs.ExecSQL;
conn2.Connected:=false;//关闭master的连接
conn.Connected:=true;
rs.Connection:=conn;
end;
end;
end;

0 0

相关博文

我的热门文章

img
取 消
img