下面是我写的代码
procedure TForm1.SpeedButton16Click(Sender: TObject);
begin
  SaveDialog1.filter:='数据库备份文件(*.bak)|*.bak';
  if SaveDialog1.Execute then
    edit18.Text := SaveDialog1.FileName+'.bak';
end;procedure TForm1.SpeedButton13Click(Sender: TObject);
begin
  OpenDialog1.filter:='数据库备份文件(*.bak)|*.bak';
  if OpenDialog1.Execute then
    edit17.Text:=OpenDialog1.FileName
  else
    edit17.Text:='';
end;procedure TForm1.SpeedButton18Click(Sender: TObject);
begin
  OpenDialog1.filter:='数据库备份文件(*.bak)|*.bak';
  if OpenDialog1.Execute then
    edit19.Text:=OpenDialog1.FileName
  else
    edit19.Text:='';
end;procedure TForm1.SpeedButton15Click(Sender: TObject);
begin
  if edit18.Text='' then
  begin
    MessageDlg('请选备份路径!',mtInformation,[mbok], 0);
  end
  else
  begin
  Label47.Caption:='请选择数据要备份的路径:'+'(正在备份数据......)';
  ADO_back.Close;
  ADO_back.SQL.Clear ;
  ADO_back.SQL.Add('Backup database xcBarCode ') ;
  ADO_back.SQL.Add('to disk='''+edit18.Text+''''+' with init');
  try
    ADO_back.ExecSQL;
    MessageDlg('备份成功!',mtInformation,[mbok], 0);
    Label47.Caption:='请选择数据要备份的路径:';
    edit18.Text:='';
  except
    MessageDlg('备份失败!',mtInformation,[mbok], 0);
    Label47.Caption:='请选择数据要备份的路径:';
  end;
  end;
end;

解决方案 »

  1.   

    你備份的時候後綴名有沒有 .bak .  你在 openDiaLog  的時候FILTER只查找 .bak  後綴的文件,可能是這個原因
      

  2.   

    问题出在edit18.Text := SaveDialog1.FileName+'.bak'; SaveDialog1.FileName已经含有后缀了,你又加了一个后缀,所以成了双后缀的,肯定就找不到了
    改成
    edit18.Text := SaveDialog1.FileName;
      

  3.   

    我的完整备份,删除,还原代码如下,大家都帮我看看有什么问题
    procedure TForm1.SpeedButton16Click(Sender: TObject);
    begin
      SaveDialog1.filter:='数据库备份文件(*.back)|*.back';
      if SaveDialog1.Execute then
        edit18.Text := SaveDialog1.FileName+'.back';
    end;procedure TForm1.SpeedButton13Click(Sender: TObject);
    begin
      OpenDialog1.filter:='数据库备份文件(*.back)|*.back';
      if OpenDialog1.Execute then
        edit17.Text:=OpenDialog1.FileName
      else
        edit17.Text:='';
    end;procedure TForm1.SpeedButton18Click(Sender: TObject);
    begin
      OpenDialog1.filter:='数据库备份文件(*.back)|*.back';
      if OpenDialog1.Execute then
        edit19.Text:=OpenDialog1.FileName
      else
        edit19.Text:='';
    end;procedure TForm1.SpeedButton15Click(Sender: TObject);
    begin
      if edit18.Text='' then
      begin
        MessageDlg('请选备份路径!',mtInformation,[mbok], 0);
      end
      else
      begin
      Label47.Caption:='请选择数据要备份的路径:'+'(正在备份数据......)';
      ADO_back.Close;
      ADO_back.SQL.Clear ;
      ADO_back.SQL.Add('Backup database xcBarCode ') ;
      ADO_back.SQL.Add('to disk='''+edit18.Text+''''+' with init');
      try
        ADO_back.ExecSQL;
        MessageDlg('备份成功!',mtInformation,[mbok], 0);
        Label47.Caption:='请选择数据要备份的路径:';
        edit18.Text:='';
      except
        MessageDlg('备份失败!',mtInformation,[mbok], 0);
        Label47.Caption:='请选择数据要备份的路径:';
      end;
      end;
    end;procedure TForm1.SpeedButton14Click(Sender: TObject);
    begin
      if edit17.Text='' then
      begin
        MessageDlg('请选还原文件!',mtInformation,[mbok], 0);
      end
      else
      begin
      Label46.Caption:='请选择要还原的数据:'+'正在还原数据......';
      if Label46.Caption='请选择要还原的数据:'+'正在还原数据......' then
        begin
          Screen.Cursor:=crHourGlass;     // Data.ADOCon.Connected:=false;
          ADO_back.Close;
          ADO_back.SQL.Clear ;
          ADO_back.SQL.Add('Use Master');
          ADO_back.SQL.Add('ALTER DATABASE xcBarCode SET OFFLINE WITH ROLLBACK IMMEDIATE');
          ADO_back.SQL.Add('RESTORE DATABASE xcBarCode') ;
          ADO_back.SQL.Add('FROM disk='''+edit17.Text+'''');
          ADO_back.SQL.Add('ALTER DATABASE xcBarCode SET ONLINE WITH ROLLBACK IMMEDIATE');
          try
            ADO_back.ExecSQL;
            MessageDlg('还原成功,请重启系统!',mtInformation,[mbok], 0);
            Label46.Caption:='请选择要还原的数据:';
            Screen.Cursor:=crDefault;
            form1.close;
          except
            MessageDlg('还原失败!',mtInformation,[mbok], 0);
            Label46.Caption:='请选择要还原的数据:';
            Screen.Cursor:=crDefault;
          end;
        end;
      end;
    end;procedure TForm1.SpeedButton17Click(Sender: TObject);
    begin
      if edit19.Text='' then
      begin
        MessageDlg('请选要删除的备份文件!',mtInformation,[mbok], 0);
      end
      else
      begin
      if MessageDlg('您是否要删除备份数据?',mtInformation,[mbyes,mbno], 0)=mryes then
        begin
          DeleteFile(edit19.Text);
          edit19.Text:='';
          SpeedButton8.Enabled:=false;
        end
      else
        exit;
      end;
    end;
      

  4.   

    那就把你得到的最终sql语句放到查询分析器中试试
      

  5.   

          ADO_back.SQL.Add('Use Master'); 
          ADO_back.SQL.Add('ALTER DATABASE xcBarCode SET OFFLINE WITH ROLLBACK IMMEDIATE'); 
          ADO_back.SQL.Add('RESTORE DATABASE xcBarCode') ; 
          ADO_back.SQL.Add('FROM disk='''+edit17.Text+''''); 
          ADO_back.SQL.Add('ALTER DATABASE xcBarCode SET ONLINE WITH ROLLBACK IMMEDIATE'); 
    这里添加完后的最终的ADO_back.SQL.Text
      

  6.   

    我查了,不过显示有错误服务器: 消息 170,级别 15,状态 1,行 2
    第 2 行: '.' 附近有语法错误。
    服务器: 消息 132,级别 15,状态 1,行 9
    标签 'Caption' 已声明。标签名称在批查询或存储过程内部必须唯一。
      

  7.   

    给你段之前写的代码,你参考参考吧。unit BackupOrRevert;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, FileCtrl, ComCtrls, ExtCtrls, Buttons, ImgList, DB,
      ADODB;type
      TFrmBackupOrRevert = class(TForm)
        GroupBox1: TGroupBox;
        RadioButton1: TRadioButton;
        RadioButton2: TRadioButton;
        Edit1: TEdit;
        GroupBox2: TGroupBox;
        Panel1: TPanel;
        ListView1: TListView;
        Label1: TLabel;
        DriveComboBox1: TDriveComboBox;
        DirectoryListBox1: TDirectoryListBox;
        FileListBox1: TFileListBox;
        SpeedButton1: TSpeedButton;
        SpeedButton2: TSpeedButton;
        ImageList1: TImageList;
        AdoBkRs: TADOQuery;
        procedure SpeedButton2Click(Sender: TObject);
        procedure RadioButton2Click(Sender: TObject);
        procedure RadioButton1Click(Sender: TObject);
        procedure DirectoryListBox1Change(Sender: TObject);
        procedure FormShow(Sender: TObject);
        procedure SpeedButton1Click(Sender: TObject);
        procedure ListView1Click(Sender: TObject);
      private
        { Private declarations }
        Files:String;
      public
        { Public declarations }
      end;var
      FrmBackupOrRevert: TFrmBackupOrRevert;implementationuses Main, GlobalUnit, PubFuntion;{$R *.dfm}procedure TFrmBackupOrRevert.SpeedButton2Click(Sender: TObject);
    begin
      close;
    end;procedure TFrmBackupOrRevert.RadioButton2Click(Sender: TObject);
    begin
      self.Label1.Visible := False;
      self.Edit1.Visible := False;
    end;procedure TFrmBackupOrRevert.RadioButton1Click(Sender: TObject);
    begin
      self.Label1.Visible := True;
      self.Edit1.Visible := True;
    end;procedure TFrmBackupOrRevert.DirectoryListBox1Change(Sender: TObject);
    var
      List_View:array[0..1]of TListItem;
      a:integer;
    begin
      ListView1.Items.Clear;   //清空备份卡列表
      If FileListBox1.Items.Count <> 0 Then
      begin
        For a:=0 to FileListBox1.Items.Count-1 do
        begin
          List_View[0]:=ListView1.Items.Add;  //添加备份卡
          List_View[0].Caption:=FileListBox1.Items.Strings[a];
          List_View[0].ImageIndex:=0;
        end;
      end;
    end;procedure TFrmBackupOrRevert.FormShow(Sender: TObject);
    var
      a:integer;
    begin
      ListView1.Items.Clear;
      If FileListBox1.Items.Count <> 0 Then
      begin      //显示当前路径下的备份卡
        For a:=0 to FileListBox1.Items.Count-1 do
        begin
          with self.ListView1.Items.Add do
          begin
            Caption:=FileListBox1.Items.Strings[a];
            ImageIndex:=0;
          end;
        end;
      end;
      Edit1.Text := FormatDateTime('yyyy-mm-dd',Now());
    end;procedure TFrmBackupOrRevert.SpeedButton1Click(Sender: TObject);
    var
      sFdir: string;
      List:TListItem;
    begin
      if RadioButton1.Checked=True then     //备份数据
      begin
        if MessageDlg('您确认要备份数据吗?',mtCustom,[mbYes,mbNo],0)=mrYes then
        begin
          if Trim(Edit1.Text) = '' then
          begin
            MessageBox(Handle,'备份文件名不能为空!','信息提示',MB_ICONWARNING + MB_OK);
            exit;
          end;
          if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
            sFdir := DirectoryListBox1.Directory+Edit1.Text+'.MqBk'
          else
            sFdir := DirectoryListBox1.Directory+'\'+Edit1.Text+'.MqBk';
          try
            AdoBkRs.ConnectionString := SADOContStr;
            AdoBkRs.Close;
            AdoBkRs.SQL.Clear;
            AdoBkRs.SQL.Text := 'Backup database MemsDb To Disk= '''+sFdir+'''';
            AdoBkRs.ExecSQL;
            AdoBkRs.Close;
          except
            on e:exception do
            begin
              MessageBox(Handle,'备份失败!','信息提示',MB_ICONWARNING + MB_OK);
              WriteLog(CUR_USER.UserID,DateTimeTostr(Now()),IntTostr(LOG_BACKUP),
                 '试图备份数据库<'+Edit1.Text+'.MqBk> 失败!');
            end;
          end;
          List:=ListView1.Items.Add;
          List.ImageIndex:=0;
          List.Caption:=Edit1.Text+'.MqBk';
          MessageBox(Handle,'备份成功!','信息提示',MB_ICONINFORMATION + MB_OK);
              {日志}
         WriteLog(CUR_USER.UserID,DateTimeTostr(Now()),IntTostr(LOG_BACKUP),
                 '备份数据库<'+Edit1.Text+'.MqBk>');
        end;
      end;
      if RadioButton2.Checked then  //还原数据库
      begin
        if MessageDlg('还原后可能会造成现有数据部分丢失'+#13+'您确认要恢复数据吗?',mtCustom,[mbYes,mbNo],0)=mrYes then
        begin
          if ListView1.Items.Count=0 then //如果为空没有记录卡
          begin
            ShowMessage('对不起没有发现备份卡,无法恢复数据.');
            Exit;
          end
          else    //如果存在记录卡
          begin
            if Files='' then   //没有选择恢复文件
            begin
              ShowMessage('对不起,请重新选择要恢复的备份.');
             Exit;
            end
          end ;
          //开始恢复
          if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
            sFdir := DirectoryListBox1.Directory+Files
          else
            sFdir := DirectoryListBox1.Directory+'\'+Files;
          try
            AdoBkRs.ConnectionString := SADOContStrMaster;//链接master
            AdoBkRs.Close;
            AdoBkRs.SQL.Clear;
            AdoBkRs.SQL.Text := 'Restore Database MemsDb From Disk= '''+sFdir+'''';
            AdoBkRs.ExecSQL;
            AdoBkRs.Close;
          except
            on e:exception do
            begin
              MessageBox(Handle,'还原失败!','信息提示',MB_ICONWARNING + MB_OK);
              WriteLog(CUR_USER.UserID,DateTimeTostr(Now()),IntTostr(LOG_BACKUP),
                 '试图还原数据库失败!');
            end;
          end;
          MessageBox(Handle,'还原完闭!','信息提示',MB_ICONINFORMATION + MB_OK);
          WriteLog(CUR_USER.UserID,DateTimeTostr(Now()),IntTostr(LOG_BACKUP),
                 '还原数据库<'+Files+'>');
        end;
      end;  
    end;procedure TFrmBackupOrRevert.ListView1Click(Sender: TObject);
    var Lists:TListItem;
    begin
     Lists:=ListView1.Selected;
     if Lists<>nil then  //选择了备份卡
     begin
       Files:=Lists.Caption;
     End
     else Files:='';
    end;end.
      

  8.   

    确认你的数据库文件已经备份了,备份后能不能找到备份的文件?还原的时候能不能在OpenDialog1对话框里面看到备份文件?
      

  9.   

    我肯定把数据库备份了,我是以bak为后缀名的,但是还原的时候在opendialog对话框里就是看不见备份文件,还有我在软件中备份数据库也找不到数据库文件,不知道怎么回事?
      

  10.   

    我选择择路径,然后点备分,显示有错误。
     说未能在sysdatabases中找到数据库xcbarcode,没有找到具有该名称的条目,请确正确输入了名称。这是什么意思 呢?
      

  11.   


        if SaveDialog1.Execute then Edit1.Text:= SaveDialog1.Files.Text;
      

  12.   

    上面的程序中备份功能,和删除都能执行了,不过还原功能还有点问题,我用备份数据库还原的时候有问题,我点还原后,弹出对话说我还原成功,但是一点确定,就显示错误,说对象名'C_user'(这是数据库中一张表)无效,不知道是什么原因/