欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

数据库备份和还原

程序员文章站 2022-06-15 17:35:25
...

1.备份 if (DirectoryExists(EdtLJ.Text))=false then // EdtLJ.Text备份绝对路径 begin ForceDirectories(EdtLJ.Text); end; strsql:=' backup database Demo to disk=' ; strsql:=strsql+#39+EdtLJ.Text+'/'+EdtMC.text+#39; //EdtMC.text备份文件名称 Dia

1.备份

if (DirectoryExists(EdtLJ.Text))=false then // EdtLJ.Text备份绝对路径
begin
ForceDirectories(EdtLJ.Text);
end;
strsql:=' backup database Demo to disk=' ;
strsql:=strsql+#39+EdtLJ.Text+'/'+EdtMC.text+#39; //EdtMC.text备份文件名称
Dialogs.ShowMessage(strsql);
with adoquery2 do
begin
close;
sql.Text:=strsql;
execSql;
showmessage('备份成功!');
end;

或者

//开始备份

with adoquery1 do
begin
close;
sql.Clear;
sql.Add('Backup database library to disk=:p1 with init');
parameters.ParamByName('p1').Value:=self.SaveDialog1.FileName;
try
Execsql;
ShowMessage('备份成功!!');
except
ShowMessage('备份失败!!');
exit;
end;
end; //End of 备份

2.还原

方法1

use master --这样避免正在使用要还原的库
go

declare cur cursor for
select spid from sysprocesses where dbid=db_id('Demo')
open cur
declare @spid int

declare @str varchar(100)

fetch next from cur into @spid
while @@fetch_status=0
begin
set @str=ltrim(@spid)
exec ('kill '+@str)
fetch next from cur into @spid
end
close cur
deallocate cur

go

restore database Demo from disk='d:/111.bak' with replace

方法2
alter database Demo set offline with rollback immediate
restore database Demo from disk='E:/1.bak' //还原语句
alter database Demo set online with rollback immediate

以上程序仅供参考,有什么不对的地方还请多多指教!