怎么在数据库中备份一张表,并且还原它,用程序实现 谢谢

解决方案 »

  1.   

    备份:Select * into 新表名 From 备份表名还原:Truncate 备份表名
          Insert into 备份表名
          Select * from 新表名
      

  2.   

    我要的是把表备份到别的地方,不是在同一个库里,我用的是ACCESS
      

  3.   

    procedure TFormMain.BackupDataClick(Sender: TObject);
    var
      MyFileName: string;
    begin
      MyFileName := '';
      try
        if SaveDialog1.Execute then
        begin
          MyFileName := SaveDialog1.FileName;
          if MyFileName <> '' then
          begin
            if CopyFile(Pchar(ExtractFilePath(Application.ExeName) + 'HYTJ.mdb'), Pchar(MyFileName), false) then
            begin
              Application.MessageBox('数据备份成功','提示',Mb_ok + mb_iconinformation);
            end;
          end;
        end;
      except
        Application.MessageBox('数据备份失败','提示',Mb_ok + mb_iconError);
      end;
      

  4.   

    procedure TFormMain.RestorDateClick(Sender: TObject);
    var
      MyFileName, aa: string;
    begin
      MyFileName := '';
      try
        if OpenDialog1.Execute then
        begin
          MyFileName := OpenDialog1.FileName;
          if MyFileName <> '' then
          begin
            aa := ExtractFilePath(Application.ExeName) + 'HYTJ.mdb';
            if CopyFile(Pchar(MyFileName),Pchar(aa), false) then
            begin
              DM.ADOConnect.Close;
              DM.ADOConnect.Open;
              Application.MessageBox('数据恢复成功','提示',Mb_ok + mb_iconinformation);
            end;
          end;
        end;
      except
        Application.MessageBox('数据恢复失败','提示',Mb_ok + mb_iconError);
      end;
      

  5.   

    用COPYFILE行吗,它不是把表的结构都复制过去了吗
      

  6.   

    1)copy表结构and数据(假如原表存在则删除)
    2)copy数据(导出以其他格式存储,这样速度不是很快)
      以前做过2种方式的,最终认为还是第1种合适
    3).....以下的接上
      

  7.   

    定义表的记录型结构
    AtableRecord=record
      fields1:DataType;
      .....
     end;function DataOutput():Boolean;
    var
      Objtable:AtableRecord;//定义 对象:
    begin
    // 从表中查出数据
    with 
    select * from table
      

  8.   

    不好意思,上面的搞错了; 
    定义表的记录型结构
    AtableRecord=record
      fields1:DataType;
      .....
     end;function DataOutput():Boolean;
    var
      D:AtableRecord;//定义 对象:
      F: File of AtableRecord;
      Filename:String;
    begin
    // 从表中查出数据
    with  query do
    begin
      close;
      sql.clear;
      sql.add('select * from table');
      open;
      AssignFile(F,FileName);
      Rewrite(F);
      if not isempty then
      begin
        while not eof  do
        begin
          with D do
          begin
            fields1:=fieldbyname('fields1').asstring; 
            .........
            Write(F,D);
            next;
          end;
        end;
      end;
      CloseFile(F);
    end;
    这是备份一个表的
    可以再写个恢复的函数,就是从保存的文件中读取数据,再写入到表中,当然要先清空该表
    具体,自己想想,我只是提供一种想法