备份数据库(50分)

  • 主题发起人 主题发起人 zouduanke
  • 开始时间 开始时间
Z

zouduanke

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi+sql2000<br>客户端怎么将数据库从服务器备份到本地
 
把服务器数据库 连接到本地sql服务器上~~<br>备份数据就行了~`
 
adoquery1连接上数据库,然后用下面的方法,就OK<br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; &nbsp; with &nbsp; adoquery1 &nbsp; do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sql.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sql.add('backup database pmcar to disk=''c:/back1.bak''');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; execsql;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; showmessage('备份完成');<br>&nbsp; except<br>&nbsp; &nbsp; &nbsp; on &nbsp; e:exception &nbsp; do<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage('备份失败');<br>&nbsp; end;<br>end; <br><br>&nbsp; ///////////////还原/////////////////////////// &nbsp; <br>procedure TForm1.BitBtn2Click(Sender: TObject);<br>begin<br>&nbsp; adoquery1.Close;<br>&nbsp; try<br>&nbsp; &nbsp; &nbsp; with &nbsp; adoquery1 &nbsp; do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sql.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sql.add('restore database pmcar from disk=''c:/back1.bak''');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; execsql;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; showmessage('还原成功');<br>&nbsp; except<br>&nbsp; &nbsp; &nbsp; on &nbsp; e:exception &nbsp; do<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage('还原失败');<br>&nbsp; end;<br>end;
 
上面是只能备份到数据库服务器上。<br>下面这个可以备份到本地<br>&nbsp; &nbsp; 下面是Adang 做的一段把SQL SERVER数据库备份到本地的代码: <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; //TODO:处理数据备份 <br>&nbsp; &nbsp; if CheckBoxDBF.Checked then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; if EditTargeDir.Text&lt;&gt;'' then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; AdoQueryBackUp:=TAdoQuery.Create(frmDataBakup); <br>&nbsp; &nbsp; AdoQueryBackup.Connection:=frmDM.ADOConSQL;//frmDM.ADOConSQL是已经连接到要备份数据库的ADO连接 <br>&nbsp; &nbsp; frmInputBox.LabelPara.Caption:='请输入本机的Administrator密码:'; <br>&nbsp; &nbsp; frmInputBox.EditInput.PasswordChar:='*'; <br>&nbsp; &nbsp; frmInputBox.Caption:='请输入密码'; <br>&nbsp; &nbsp; if frmInputBox.ShowModal=mrok then <br>&nbsp; &nbsp; AdminPW:=frmInputBox.EditInput.Text; <br>&nbsp; &nbsp; //备份远程SQL Server到本地 <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; Winexec(pchar('net share test='+ExtractFileDir(EditTargeDir.Text)),SW_HIDE);//共享本地保存数据的文件夹,这是必要的 <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; on E:Exception do <br>&nbsp; &nbsp; Application.MessageBox(pchar(E.Message),'Net share ERROR'); <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; Winexec(pchar('net use //'+PublicElement.LocalIP+'/test '+AdminPW+' /user:domain/Administrator'),SW_HIDE);//连接到刚才的共享,LocalIP是取得当前计算机IP的函数 <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; on E:Exception do <br>&nbsp; &nbsp; Application.MessageBox(pchar(E.Message),'Net Use ERROR'); <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; AdoQueryBackup.SQL.Clear; <br>&nbsp; &nbsp; AdoQueryBackup.SQL.Text:='backup database YYFD to disk='+''''+'//'+PublicElement.LocalIP+'/test/'+ExtractFileName(EditTargeDir.Text)+'''';//执得备份SQL语句 <br>&nbsp; &nbsp; AdoQueryBackup.ExecSQL; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; on E:Exception do <br>&nbsp; &nbsp; Application.MessageBox(pchar(E.Message),'Backup ERROR'); <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; //备份结束 <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; on E:Exception do <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; Application.MessageBox(pchar(E.Message),'数据库备份出错') ; <br>&nbsp; &nbsp; Raise; <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; finally <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; AdoQueryBackup.Close; <br>&nbsp; &nbsp; AdoQueryBackup.Free; <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; Application.MessageBox('数据库备份已经成功完成!','操作成功'); <br>&nbsp; &nbsp; end <br>&nbsp; &nbsp; else <br>&nbsp; &nbsp; if CheckBoxSQL.Checked then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; //TODO:将数据备份到指定其他SQL服务器中 <br>&nbsp; &nbsp; Application.MessageBox('程序暂时没有提供备份到其他服务器的功能!','提示'); <br>&nbsp; &nbsp; end;
 
后退
顶部