hoge66的专栏

-------------------- 海纳百川而深广,山容万木而葱茏!

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

备份:
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;

阅读更多
个人分类: deiphi技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭