当前位置:Gxlcms > mysql > 数据库备份和还原

数据库备份和还原

时间:2021-07-01 10:21:17 帮助过:37人阅读

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

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

人气教程排行