try
ADOCommand1:=TADOCommand.Create(nil);
ADOCommand1.Connection:=FRM_MAIN.FmainADOConnection;
ADOCommand1.CommandText:='ALTER DATABASE RedStar SET OFFLINE WITH ROLLBACK IMMEDIATE';
ADOCommand1.Execute;
ADOCommand1.CommandText:='RESTORE DATABASE RedStar FROM DISK = '''+reportconfig.Restore_Path+'''';
ADOCommand1.Execute;
MessageBox(0,'数据库恢复成功!','提示信息,MB_OK);
finally
ADOCommand1.CommandText:='ALTER DATABASE RedStar SET ONLINE WITH ROLLBACK IMMEDIATE';
ADOCommand1.Execute;
ADOCommand1.Free;
end;
提示错误:用户必须在master数据库中
我这样写数据库恢复语句对吗,如果不对请各位高手指点.
恢复数据库的时候要中断此数据库的所有连接,并将当前数据库设置为master,否则就会恢复失败
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[killspid]
GO
create proc killspid(@dbname varchar(20))
as
begin
declare @sql nvarchar(500)
declare @spid int
set @sql='declare getspid cursor for
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
exec('kill '+@spid)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
GO
exec killspid 'yourdb'
GO
try
QryRDB:=TADOQuery.Create(Nil);
QryRDB.Connection:=ADOConnection1;
RestoreDatabaseFrm.Caption:='Database restoring...';
with QryRDB do
begin
Close; //切断数据库连接
SQL.Clear;
SQL.Add('ALTER DATABASE RedStar SET OFFLINE WITH ROLLBACK IMMEDIATE');
ExecSQL;
Close;
SQL.Clear;
SQL.Add('Restore Database RedStar From Disk=:a With Replace');
Parameters[0].Value:=edt_DBPath.Text;
ExecSQL;
martin_messagebox(RS_INF_RestoreSaveOk,0);
RestoreDatabaseFrm.Caption:='Restore Database';
end;
finally
ADOConnection1.Connected:=False;
ADOConnection1.Close;
QryRDB.Free;
end;
这是我写的程序,可以恢复。但是恢复完后,访问别的数据就提示:连接失败,关闭应用程序后,又可以访问了,请各位高手麻烦在我写的程序上给指点一下,怎样恢复完后不用关闭应用程序呢!!!