请问大虾如何用delphi7备份和恢复mssql2000数据库,谢谢!

解决方案 »

  1.   

    //备份
    procedure TForm23.Button1Click(Sender: TObject);
    begin
    if savedialog1.Execute then
    begin
      adoquery1.SQL.Text:='backup database mydatabase to disk='''+savedialog1.FileName+''' with init';
      adoquery1.ExecSQL;
      application.MessageBox('数据库备份成功','警告',MB_OK);
    end;
    end;//恢复
    在查询分析器中运行
    use master
    goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_killspid]
    GOcreate proc p_killspid
    @dbname sysname--要关闭进程的数据库名
    as  
    declare @s nvarchar(1000)
    declare tb cursor local for
    select s='kill '+cast(spid as varchar)
    from master..sysprocesses 
    where dbid=db_id(@dbname)open tb 
    fetch next from tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch next from tb into @s
    end
    close tb
    deallocate tb
    goprocedure TForm23.Button2Click(Sender: TObject);
    begin
    if opendialog1.Execute then
    begin
    adoquery1.SQL.Text:='use master;exec p_killspid mydatabase;Restore database madatabase From disk='''+opendialog1.FileName+''';Use mydatabase';
    adoquery1.ExecSQL;
    application.MessageBox('数据库完成恢复','警告',MB_OK);
    end;
    end;
      

  2.   

    备份:
    procedure TF_bak.GradBtn2Click(Sender: TObject);
    var adobackup:TAdocommand;
    begin
     if Edit1.Text='' then
     begin
     Application.MessageBox('请选择备份文件的存放路径!','提示',64);
     exit;
     end;
     adobackup:=TAdocommand.Create(application);
     adobackup.ConnectionString:=' Provider=SQLOLEDB.1;'+'Persist Security Info=False;User ID='+GetRegInfo(3)+';Password='+GetRegInfo(4)+';'+
                      'Initial Catalog='+GetRegInfo(2)+';Data Source='+GetRegInfo(1); adobackup.CommandText:='backup database RenSalMan to disk='''+Edit1.text+'''';
     try
      adobackup.Execute;
      Application.MessageBox('备份成功!','提示',64);
      adobackup.Free;
      close;
     except
      Application.MessageBox('备份失败!','提示',64);
      adobackup.Free;
      exit;
     end;
    end;
      

  3.   

    恢复:
    procedure TF_restore.GradBtn1Click(Sender: TObject);
    var adorestore:TAdocommand;
    begin
     if Edit1.Text='' then
     begin
     Application.MessageBox('请选择恢复文件!','提示',64);
     exit;
     end;
      if application.MessageBox('此操作将使上次备份以来的所有数据丢失,是否继续?','恢复数据',33)=IDCANCEL then  exit;
             DM.Free;
             adorestore:=TAdocommand.Create(application);
             adorestore.ConnectionString:=' Provider=SQLOLEDB.1;'+'Persist Security Info=False;User ID='+GetRegInfo(3)+';Password='+GetRegInfo(4)+';'+
                      'Data Source='+GetRegInfo(1);
             adorestore.CommandText:='restore database RenSalMan from disk='''+Edit1.Text+''' with replace';
             try
              adorestore.Execute;
              adorestore.Free;
              if Application.MessageBox( '恢复成功! 现在需要重新登陆软件,请按“确定” ','提示',64)=IDOK then application.Terminate;
             except
              Application.MessageBox('恢复失败!请确认恢复文件的路径和名称是否正确,或是否已关闭其它正在使用此数据库的程序!','提示',64);
              adorestore.Free;
              exit;
             end;//end try
    end;
      

  4.   

    backup database mydatabase to disk='......'