各位大侠帮帮忙~我们的毕业设计系统是三个人一个小组的,前期大家都是各自做各自的模块,连接数据库有的用ADOQuery,table,Query,各不相同,现在我想做一个系统数据库备份和还原的功能,在还原数据库上出了问题,是不是还原数据库必须是要把所有连接原数据库的程序都关掉?要怎么做才能把多个table,query的连接断开后再还原数据库???期待大家的回答~谢谢`~~~
解决方案 »
- 如何让在主程序上始终显示一条线?
- 如何将Image中的图片按照指定的尺寸打印出来
- ***delphi+access的程序在繁体XP上运行???****
- 又有问题了,Hex到汉字和英文同时输出!(中国人帮忙顶了)
- 哪里有这样的源码或组件:类似IDE,点击form上的元件,会显示被选择(8个黑点),可以move,resize???
- FR做的报表中:打印时,单击windows桌面的右下角图标弹出......
- 如何向SQL Server中添加图片字段!
- 如何用Delphi写一个包含窗体的DLL,而且该DLL给VC调用时,DLL中的窗体不出现在任务栏上
- 为什么我在dll代码中加入一句showmessage('Good!')后编译会出错?
- 请问大虾:哪儿有使用delphi开发CTI,IVR方面的图书、资料或源代码?图书的名字或网址叫什么?
- 关于CRC计算,请高手指点!
- 请教mysql的数据备份怎么做!用程序作语句怎么写,我写的SQL语句怎么不对,请大家帮我看看,谢谢了!!!
还原的话用Restore DataBase Pubs from disk='c:\qqq.bak'
你备份的时候,可以直接执行该语句,但是你还原的时候,需要关闭其他连接,有个好的方法,就是你们三个需要统一起数据库的连接方式,可以共用一个数据模块,建议使用ADO来连接数据库,这样的话,需要关闭数据库,只需要adoconnection1.close;就可以了,你可以先试一下.
procedure TBackupFrm.BitBtn2Click(Sender: TObject);
begin
if Edit1.Text= '' then
begin
Showmessage('无选择要保存的文件名');
exit;
end;
try
try
dmData.adoQryTmp.Active:= false;
dmData.adoQryTmp.SQL.Clear;
dmData.adoQryTmp.SQL.Add('BACKUP DATABASE [dzyl] TO DISK = '''+edit1.text+''' WITH INIT');
dmData.adoQryTmp.ExecSQL;
finally
begin
dmData.adoQryTmp.Active:= false;
Showmessage('数据库备份成功!');
end;
end;
except
on e:exception do
begin
ShowMessage('数据库备份失败!');
end;
end;
end;
---------------
恢复
procedure TBackupFrm.BitBtn4Click(Sender: TObject);
begin
if Edit2.Text = '' then
begin
showmessage('未选择要恢复的数据库文件!');
exit;
end;
with dmData do
begin
try
adocmmd.CommandText:='use master';
adocmmd.Execute;
adocmmd.CommandText:=' alter database dzyl set offline with rollback immediate';
adocmmd.Execute;
adocmmd.CommandText:='restore database dzyl from disk= '''+edit2.Text+''' with recovery ';
adocmmd.Execute;
adocmmd.CommandText:=' alter database dzyl set online with rollback immediate';
adocmmd.Execute;
showmessage('数据库恢复成功!');
application.Terminate;
except
on e:exception do
begin
showmessage('数据库恢复失败!'+e.Message);
end; end;
end;
end;
dmData.adoQryTmp连接的是系统MASTER数据库,备份还原之前应该关闭要备份还原的AdoConn数据库连接
AdoConn.Connected:=False;
我想先试一试其他办法,最后不行的话再大家一起改连接数据库的方式~~
我想用杀进程的方法,有人懂吗?谢谢大家了~
create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500),@temp varchar(1000)
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 =0
begin
set @temp='kill '+rtrim(@spid)
exec(@temp)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
endGO=================
杀死数据库进程
下面的例子是我贴来的,不知道能不能用,不过大概可以起个引导作用。procedure TBak.btnRestoreClick(Sender: TObject);
var
Qry:TADOQuery;
begin
if FileExists(self.edtReStorePath.Text) then
begin
DM.Conn.Connected:=False; //断开系统数据库连接
Qry:=TADOQuery.Create(self);
Qry.CommandTimeout:=120;
//把连接转移到master库
Qry.ConnectionString:='Provider=SQLOLEDB.1;Password='+Login.GetDBServerInfo('Pwd')+';Persist Security Info=True;User ID='+Login.GetDBServerInfo('User')+';Initial Catalog=master;Data Source='+Login.GetDBServerInfo('Server');
//-------------------端开rxd_CRM库------------------------------------------
Qry.Close;
Qry.SQL.Clear;
Qry.SQL.Text:='ALTER DATABASE rxd_CRM SET OFFLINE WITH ROLLBACK IMMEDIATE';
Qry.Prepared;
Qry.ExecSQL;
//------------------- 端开rxd_CRM库结束-------------------------------------
Qry.Close;
Qry.SQL.Clear;
Qry.SQL.Text:='RESTORE DATABASE rxd_CRM FROM DISK='''+self.edtReStorePath.Text+''' with replace';
Qry.Prepared;
try
Qry.ExecSQL;
Login.MsgInfo('成功恢复数据!');
finally
//-------------------重新连接rxd_CRM库------------------------------------
Qry.Close;
Qry.SQL.Clear;
Qry.SQL.Text:='ALTER DATABASE rxd_CRM SET ONLINE WITH ROLLBACK IMMEDIATE';
Qry.Prepared;
Qry.ExecSQL;
//-------------------重新连接rxd_CRM库结束--------------------------------
Qry.Free;
DM.Conn.ConnectionString:=Login.GetDBServerInfo('DBStr');
try
DM.Conn.Connected:=True;
except
Login.MsgError('连接数据库失败,请尝试重启系统!');
end;
end
end
else
begin
Login.MsgInfo('数据文件不存在,请重新选择!');
self.edtReStorePath.SetFocus;
end;
end;