sqlserver 数据库备份 88分全呈上(88分)

  • 主题发起人 主题发起人 gongfs
  • 开始时间 开始时间
G

gongfs

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:<br> &nbsp; &nbsp;我写了一段,数据库备份和恢复的程序:<br> &nbsp;发现能备份数据库和恢复数据库,但内容不么生变化<br> &nbsp;比如:我现在将数据库备上,然后在数据库中再添加一条记录1<br> &nbsp; 这时恢复数据库,发现记录1还在数据库中<br>请指点,88分全呈上<br> &nbsp; procedure Tsjwh.BitBtn1Click(Sender: TObject);<br> &nbsp;var qrystr,tempstr:string;<br>begin<br> &nbsp;if trim(bftx1.Text)=''<br> &nbsp; &nbsp;then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;messagebox(handle,pchar('请先输入备份数据库目录!'),pchar('信息!'),64);<br> &nbsp; &nbsp; &nbsp; &nbsp;bftx1.SetFocus;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin &nbsp;//1<br> &nbsp; &nbsp; &nbsp; &nbsp; qrystr:='';tempstr:='';<br> &nbsp; &nbsp; &nbsp; &nbsp; tempstr:=trim(bftx1.Text);<br> &nbsp; &nbsp; &nbsp; &nbsp; qrystr:=' BACKUP DATABASE cg TO disk='''+tempstr+''''+' with init,stats';<br> &nbsp; &nbsp; &nbsp; &nbsp; adotemp.Close;<br> &nbsp; &nbsp; &nbsp; &nbsp; adotemp.SQL.Clear;<br> &nbsp; &nbsp; &nbsp; &nbsp; adotemp.SQL.Add(qrystr);<br> &nbsp; &nbsp; &nbsp; &nbsp; try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adotemp.ExecSQL<br> &nbsp; &nbsp; &nbsp; &nbsp; finally<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; messagebox(handle,pchar('数据库备份成功!'),pchar('信息!'),64);<br> &nbsp; &nbsp; &nbsp;end;//1<br><br>end;<br><br>procedure Tsjwh.BitBtn3Click(Sender: TObject);<br> &nbsp;var qrystr,tempstr,tempstr1:string;<br> &nbsp; &nbsp; &nbsp;len:integer;<br> &nbsp; &nbsp; &nbsp; kg:integer;<br>begin<br> &nbsp; kg:=0;<br> &nbsp; kg:=messagebox(handle,pchar('您确定恢复数据库吗?'),pchar('是否恢复?'),mb_yesNo+mb_iconquestion+mb_defbutton2);<br><br> &nbsp; if kg=6<br> &nbsp; &nbsp; then<br> &nbsp; &nbsp; &nbsp; begin // &nbsp;恢复1<br> &nbsp;if trim(hftx1.Text)=''<br> &nbsp; &nbsp;then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;messagebox(handle,pchar('请先输入备份数据库的目录!'),pchar('信息!'),64);<br> &nbsp; &nbsp; &nbsp; &nbsp;hftx1.SetFocus;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin &nbsp;//1<br> &nbsp; &nbsp; &nbsp; &nbsp; qrystr:='';tempstr:=''; &nbsp; tempstr1:='';<br> &nbsp; &nbsp; &nbsp; &nbsp; tempstr:=trim(hftx1.Text);<br> &nbsp; &nbsp; &nbsp; &nbsp; len:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; len:=length(tempstr)-3;<br> &nbsp; &nbsp; &nbsp; &nbsp; tempstr1:=copy(trim(hftx1.Text),len,4);<br> &nbsp; &nbsp; &nbsp; &nbsp; showmessage(tempstr1);<br> &nbsp; &nbsp; &nbsp; &nbsp; if tempstr1&lt;&gt;'.dat'<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin &nbsp;//11<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messagebox(handle,pchar('请输入有效的数据库文件!'),pchar('信息!'),64);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hftx1.SetFocus;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end//11<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin &nbsp; //22<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tempstr:=trim(hftx1.Text);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;qrystr:='use cg restore database cg from disk='''+tempstr+'''';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adotemp.Close;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adotemp.SQL.Clear;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adotemp.SQL.Add(qrystr);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adotemp.ExecSQL<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;messagebox(handle,pchar('数据库恢复成功!'),pchar('信息!'),64);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;//22<br><br><br> &nbsp; &nbsp; &nbsp;end;//1<br> &nbsp; &nbsp;end;//恢复2<br><br><br>end;
 
恢复的时候,你不应该连接当前要恢复的数据库。
 
将此句代码:<br>qrystr:='use cg restore database cg from disk='''+tempstr+'''';<br>换成:qrystr:='use cg restore database cg from disk='''+tempstr+''' with Replace ';<br>将现有数据库强制还原
 
yanzhaozhizi的是正解<br>另外,是不是要改为use master
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
381
import
I
后退
顶部