这个我也遇到过,后来解决了,你先建立存储过程,在master数据库中 CREATE proc restoredatabase @dbname varchar(200) --要关闭进程的数据库名 , @filename varchar(500) --要还原的数据库备份的地址 as declare @sql nvarchar(500) declare @spid nvarchar(20) declare #tb cursor for select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) open #tb fetch next from #tb into @spid while @@fetch_status=0 begin exec('kill '+@spid) fetch next from #tb into @spid end close #tb deallocate #tb restore database YHK_WaterL from disk=@filename GO然后在Delphi程序里调用如下: try //检查存储过程 Str:='select 1 from sysobjects where name=''restoredatabase'' and objectproperty(id,''IsProcedure'')=1'; QrOpenSql(ADOQuery1,Str); if ADOQuery1.Eof then begin Application.messagebox(pchar('数据库发生错误:存储过程丢失!'),'提示信息',Mb_ok + Mb_IconError); exit; end; ComputerName:='server';//用来存计算机名 DatabaseName:='yourdatabase'; with ADOQuery1 do begin close; sql.Clear; sql.Add('use master exec restoredatabase '+DatabaseName+','''+trim(Edt_Path.Text)+''''); execsql; end; except Application.MessageBox('未预料的情况发生,数据恢复失败','数据恢复失败',MB_IconInformation); exit; end;
Alter DataBase DBName SET ONLINE WITH ROLLBACK IMMEDIATE
但必须用:ALTER DATABASE DB_CLE SET ONLINE WITH ROLLBACK IMMEDIATE还原
adoquery1.SQL.text:='use master';
adoquery1.ExecSQL;
这样就可以转移到master数据库,下面就是你自己的恢复数据库语句
CREATE proc restoredatabase
@dbname varchar(200) --要关闭进程的数据库名
, @filename varchar(500) --要还原的数据库备份的地址
as
declare @sql nvarchar(500)
declare @spid nvarchar(20) declare #tb cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #tb
fetch next from #tb into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #tb into @spid
end
close #tb
deallocate #tb
restore database YHK_WaterL from disk=@filename
GO然后在Delphi程序里调用如下:
try
//检查存储过程
Str:='select 1 from sysobjects where name=''restoredatabase'' and objectproperty(id,''IsProcedure'')=1';
QrOpenSql(ADOQuery1,Str);
if ADOQuery1.Eof then
begin
Application.messagebox(pchar('数据库发生错误:存储过程丢失!'),'提示信息',Mb_ok + Mb_IconError);
exit;
end;
ComputerName:='server';//用来存计算机名
DatabaseName:='yourdatabase';
with ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('use master exec restoredatabase '+DatabaseName+','''+trim(Edt_Path.Text)+'''');
execsql;
end;
except
Application.MessageBox('未预料的情况发生,数据恢复失败','数据恢复失败',MB_IconInformation);
exit;
end;