在delphi7中用程序如何实现指定路径来备份和导入SQL SERVER数据库???????

解决方案 »

  1.   

    function TfrmMain.DateOption(vInt: Integer): Boolean;
    var
      vSQL : String;
      DataPath : String;
    begin
      DataPath := GetDataPath(vInt);
      if DataPath <> '' then
        if vInt = 0 then
        begin
          vSQL := 'BACKUP DATABASE Bar TO DISK = ''' + ChangeFileExt(DataPath,'.BAK') + '''';
          DataExecute(vSQL);
        end else
        begin
          dmMain.ADOConnection1.Connected := False;
          vSQL :=  'ALTER DATABASE Bar SET OFFLINE WITH ROLLBACK IMMEDIATE';
          DataExecute(vSQL);
          vSQL :=   'RESTORE DATABASE Bar FROM DISK = ''' + DataPath + '''';
          DataExecute(vSQL);
          vSQL :=   'ALTER DATABASE Bar SET ONLINE WITH ROLLBACK IMMEDIATE';
          DataExecute(vSQL);
        end;
    end;
      

  2.   

    其中:
    function TfrmMain.GetDataPath(vInt: Integer): String;
    var
      PathForm : TPathForm;
    begin
      Result  := '';
      Try
        PathForm := TPathForm.Create(Nil);
        if vInt = 0 then
        begin
          PathForm.Caption := '请选择数据库备份路径';
          PathForm.Hint := 'Backup';
        end else
        begin
          PathForm.Caption := '请选择数据库恢复路径';
          PathForm.Hint := 'Restory';
        end;
        PathForm.ModalResult := mrCancel;
        PathForm.ShowModal;
      Finally
        Result := PathForm.FPath;
        FreeAndNil(PathForm);
      end;
    end;function TfrmMain.DataExecute(vSQLStr: String): Boolean;
    begin
      with DMMain.ADOCommand1 do
      begin
        CommandText := vSQLStr;
        Execute;
      end;
    end;
      

  3.   

    'BACKUP DATABASE Bar TO DISK = ''' + ChangeFileExt(DataPath,'.BAK') + ''''
      

  4.   

    基本思想是:在一个Form中,分别用两个TDatabase控件连接新老数据库。并采用TTable、TDbGrid作为数据转移的中心,根据DbGrid中的数据生成标准的SQL插入语句。这样,就实现了从一个数据库系统到另一个数据库系统的数据转移。在这里,采用TTable、TDbGrid作为数据转移的中心是一个技巧,因为:TTable的Fields属性能指示出某字段的字段名称、数据类型等,这为数据转移过程中的Insert语句的生成及数据类型转换提供了依据。上面的思想从Foxpro到SQL Server的数据转移方法的方法是可以实现数据转移的其他数据库间也行,1000条数据在时间等待上还可以承受,但是,20万条上百万条数据的转移客户就要骂人了,一天、两天。如何才能提高执行速度?Access倒入20万条数据到SQL2000耗时1分,用上述“思想”的编程程序却长达3个多小时?请问有何良策?
      

  5.   

    数据库备份按钮Button1的单击事件如下:procedure TForm1.Button1Click(Sender: TObject);begin  if Edit1.Text<>'' then    begin      self.Caption := '正在备份数据库,请稍候....';      ADOCommand1.CommandText:= 'backup database HR to disk=''' + Edit1.Text + ''''; //备份数据库命令语句      try        ADOCommand1.Execute;  // 执行备份        MessageDlg('数据库备份成功!',mtInformation,[mbOK],0);        Self.Close;      except        MessageDlg('数据库备份失败!',mtConfirmation,[mbOK],0);        Self.Close;      end;    end  else    MessageDlg('请输入正确的备份路径及文件名!',mtConfirmation,[mbYes],0);end; 数据库恢复按钮Button2的单击事件如下:procedure TForm.Button2Click(Sender: TObject);begin   if OpenDialog1.Execute then    begin      self.Caption := '正在还原数据库,请稍候....';      try        ADOConnection1.Connected:=False;//关闭与要恢复的数据库连接        //打开master数据库(事先将ADOConnection2连接到Master数据库)        ADOConnection2.Connected:=True;                ADOCommand1.Connection:=ADOConnection2;//转移连接到数据库master        ADOCommand1.CommandText:='ALTER DATABASE HR SET OFFLINE WITH ROLLBACK       IMMEDIATE';//切断要恢复的数据库,”HR” 是你要恢复的数据库的名字!        ADOCommand1.Execute;        ADOCommand1.CommandText:='RESTORE DATABASE HR FROM DISK = '''  +opendialog1.FileName +''' WITH REPLACE';//恢复要恢复的数据库        try          ADOCommand1.Execute;          MessageDlg('数据库恢复完成!',mtInformation,[mbOK],0);        except          MessageDlg('数据库恢复失败!',mtConfirmation,[mbOK],0);        end;      finally        ADOCommand1.CommandText:='ALTER DATABASE HR SET ONLINE WITH ROLLBACK IMMEDIATE';    //重新连接恢复好的数据库        ADOCommand1.Execute;        ADOConnection2.Connected:=False;//关闭master数据库        ADOConnection1.Connected:=True;//打开与恢复数据库的连接        ADOCommand1.Connection:=ADOConnection1;//恢复连接到数据库      end;      Self.Close;    end;end;
    我也是从网上搜的别人的,然后整理了一下,我做的东西里就是这么用的,可以实现!
    你试一试!
      

  6.   

    请问这个空件ADOCommand1的属性需要设置吗??