i:=0; try try ADOConnection1.Open; if OpenDialog1.Execute then begin with AdoCommand1 do begin CommandText:='use Master'; Execute; CommandText:='execute sp_helpdevice'; Execute; CommandText:='Restore database cjk From disk='''+OpenDialog1.FileName+''' with replace'; Execute ; end; end; ADOConnection1.Close; except ShowMessage('导入数据失败,请将该数据库关闭!'); i:=1; end; finally if i=0 then ShowMessage('导入数据成功!'); end;
To xiong1979 怎么实现程序的备份哦,急需要的啊,可不可以吧数据备份的程序看一下啊,谢谢等待
procedure TDBBackupForm.XP_Button2Click(Sender: TObject); begin pgbar.Max:=100; pgbar.Min:=0; pgbar.Position:=0; pgbar.Step:=20; With SaveDialog1 do begin filename:='DB'+FormatDateTime('yy_MM_dd_hh_mm_ss',Now); options:=[ofhidereadonly,offilemustexist,ofpathmustexist]; if execute then begin label1.Caption:='正在备份中...'; DataModule1.ADOConnection1.Close; TmpStr:= ExtractFileName(filename); strlen:= Length(TmpStr); DevName:= Copy(TmpStr,1,strlen-4); begin try ADOCommand1.CommandText:='use Master'; pgbar.StepIt; ADOCommand1.Execute; adocommand1.CommandText:='execute sp_helpdevice'; pgbar.StepIt; adocommand1.Execute ; pgbar.stepit; adocommand1.CommandText:='backup database NewCrmBXN to disk='''+filename+''' with init'; pgbar.StepIt; adocommand1.Execute ; adocommand1.CommandText:='Use NewCrmBXN'; pgbar.StepIt; adocommand1.Execute ; Self.Hide; ShowMessage('数据库已经成功备份到 '+filename+'!'); Try DataModule1.ADOConnection1.Connected:= True; Except ShowMessage('无法重新接连数据库!,系统即将退出!'); Application.Terminate; End; self.Close; Except On Exception do if DataModule1.ADOConnection1.InTransaction then DataModule1.ADOConnection1.RollbackTrans; End; End; end; end;end;
楼上两位写的这两种方式在单独和数据连接时,都可以, 可在要还原的数据库已经与ADOConnection连接了,就算只有一ADOConnection 并而它现在由连接"要恢复的数据库"变为连接Master了,在执行时, 还是会出这样的错:DataBase in use,The system administrator must have exclusive use of the DataBase to run restore operation. 这问题怎么解决?
if PinMaDm.ConnectBackup(aserverName,auserName,aPassword){连接数据库的过程} then try if SaveDialog1.Execute then with Pinmadm.ADOcommand do begin CommandText:='backup DataBase PinMa(数据库) to Disk='+''''+saveDialog1.FileName+''''; Execute; end; Finally try PinMaDm.ConnectDB(aserverName,auserName,aPassword,'PinMa'); except abort; end;
try
try
ADOConnection1.Open;
if OpenDialog1.Execute then
begin with AdoCommand1 do
begin
CommandText:='use Master';
Execute;
CommandText:='execute sp_helpdevice';
Execute;
CommandText:='Restore database cjk From disk='''+OpenDialog1.FileName+''' with replace';
Execute ;
end;
end;
ADOConnection1.Close;
except
ShowMessage('导入数据失败,请将该数据库关闭!');
i:=1;
end;
finally
if i=0 then ShowMessage('导入数据成功!');
end;
怎么实现程序的备份哦,急需要的啊,可不可以吧数据备份的程序看一下啊,谢谢等待
begin
pgbar.Max:=100;
pgbar.Min:=0;
pgbar.Position:=0;
pgbar.Step:=20;
With SaveDialog1 do
begin
filename:='DB'+FormatDateTime('yy_MM_dd_hh_mm_ss',Now);
options:=[ofhidereadonly,offilemustexist,ofpathmustexist];
if execute then
begin
label1.Caption:='正在备份中...';
DataModule1.ADOConnection1.Close;
TmpStr:= ExtractFileName(filename);
strlen:= Length(TmpStr);
DevName:= Copy(TmpStr,1,strlen-4);
begin
try
ADOCommand1.CommandText:='use Master';
pgbar.StepIt;
ADOCommand1.Execute;
adocommand1.CommandText:='execute sp_helpdevice';
pgbar.StepIt;
adocommand1.Execute ;
pgbar.stepit;
adocommand1.CommandText:='backup database NewCrmBXN to disk='''+filename+''' with init';
pgbar.StepIt;
adocommand1.Execute ;
adocommand1.CommandText:='Use NewCrmBXN';
pgbar.StepIt;
adocommand1.Execute ;
Self.Hide;
ShowMessage('数据库已经成功备份到 '+filename+'!');
Try
DataModule1.ADOConnection1.Connected:= True;
Except
ShowMessage('无法重新接连数据库!,系统即将退出!');
Application.Terminate;
End;
self.Close;
Except
On Exception do
if DataModule1.ADOConnection1.InTransaction then
DataModule1.ADOConnection1.RollbackTrans;
End;
End;
end;
end;end;
是什么意思哦??
相信有用
可在要还原的数据库已经与ADOConnection连接了,就算只有一ADOConnection
并而它现在由连接"要恢复的数据库"变为连接Master了,在执行时,
还是会出这样的错:DataBase in use,The system administrator
must have exclusive use of the DataBase to run restore operation. 这问题怎么解决?
try
if SaveDialog1.Execute then
with Pinmadm.ADOcommand do
begin
CommandText:='backup DataBase PinMa(数据库) to Disk='+''''+saveDialog1.FileName+'''';
Execute;
end;
Finally
try
PinMaDm.ConnectDB(aserverName,auserName,aPassword,'PinMa');
except
abort;
end;