⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backup_restore.pas

📁 清华大学出版社出版的数据库系统教程附带的实验教程中所有的源代码
💻 PAS
字号:
unit backup_restore;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList, ComCtrls, FileCtrl, StdCtrls, Buttons, DB, ADODB,QFileCtrls,ShellAPi;

type
  TForm1 = class(TForm)
    Lab_Panel1: TLabel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    BitBtn1: TBitBtn;
    GroupBox2: TGroupBox;
    ListView1: TListView;
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    StatusBar1: TStatusBar;
    ImageList1: TImageList;
    ADOConnection1: TADOConnection;
    procedure BitBtn1Click(Sender: TObject);
    procedure ListView1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure StatusBar1DrawPanel(StatusBar: TStatusBar;
      Panel: TStatusPanel; const Rect: TRect);
    procedure DriveComboBox1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    Files:String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   backupflag:string;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
conn:TADOConnection;
HF:TADOCommand;
BF:TADOQuery;
List:TListItem;
a:integer;
sPath,sFileName:String;
begin
  if (RadioButton1.Checked=true)and (RadioButton2.Checked=false)  then         //备份数据
    begin   //1
      if MessageDlg('您确认要备份数据吗?',mtCustom,[mbYes,mbNo],0)=mrYes then
        begin   //2
          if (ListView1.Items.Count<>0 )then          //存在记录
            begin //3
              For a:=0 to ListView1.Items.Count-1 do
                begin  //4
                  DeleteFile('children.bak');
                end;   //4  结束for循环
                try
                ListView1.Items.Clear;
                BF:=TADOQuery.Create(Self);
                BF.Connection:=adoconnection1;
                BF.Close;
                BF.SQL.Clear;
                BF.SQL.Text:='Backup DataBase children to disk=:A';
                if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
                begin
                BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+Label2.Caption ;
                end
                else
                begin
                BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+'\'+Label2.Caption;
                end;
                BF.ExecSQL;
                BF.Close;
                BF.Free;
                adoconnection1.Connected:=false;
                List:=ListView1.Items.Add;
                List.ImageIndex:=0;
                List.Caption:=Label2.Caption;
                Lab_panel1.Caption:='备份完毕,旧备份已覆盖';
                Lab_panel1.Font.Color:=clWindowText;
                except
                MessageDlg('数据库备份失败,请确认程序是否连接到本机数据库且用系统管理员身份登陆本系统。',mtInformation ,[mbOK],0);
                exit;
                end;
            end //3
          else            //该目录下没有备份记录
            begin
             // ADOConnection1.Connected:=false;
              try
              BF:=TADOQuery.Create(Self);
              BF.Connection:=adoconnection1;
              BF.Close;
              BF.SQL.Clear;
              BF.SQL.Text:='Backup DataBase children to disk=:A';
              if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
              BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+Label2.Caption
              else
              BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+'\'+Label2.Caption;
              BF.ExecSQL;
              BF.Close;
              BF.Free;
              List:=ListView1.Items.Add;
              List.ImageIndex:=0;
              List.Caption:=Label2.Caption;
              Lab_panel1.Caption:='备份完毕';
              Lab_panel1.Font.Color:=clWindowText;
              except
              MessageDlg('数据库备份失败,请确认程序是否连接到本机数据库且用系统管理员身份登陆本系统。',mtInformation ,[mbOK],0);
              exit;
              end;
            end;
        end;   //2
    end;   //1


   if (RadioButton2.Checked=True) and (RadioButton1.Checked=false) then   //选择恢复数据
     begin
       if MessageDlg('您确认要恢复数据吗?',mtCustom,[mbYes,mbNo],0)=mrYes then
         begin
           if ListView1.Items.Count=0 then //如果为空没有记录卡
             begin
               Lab_panel1.Caption:='对不起没有发现备份卡,无法恢复数据';
               Lab_panel1.Font.Color:=clred;
               Exit;
             End
           else    //如果存在记录卡
             begin
               if Files='' then   //没有选择恢复文件
                 begin
                   Lab_panel1.Caption:='对不起,请重新选择要恢复的备份';
                   Lab_panel1.Font.Color:=clred;
                   Exit;
                 End
               Else
                 begin    //开始恢复
                   backupflag:='1';
                   try
                   conn:= TADOConnection.Create(nil);
                   conn.LoginPrompt:= false;
                   conn.Connected:= false;
                   conn.ConnectionString:='Provider=SQLOLEDB.1;Password=1234;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=(local)\children';

                    adoconnection1.Free;             //关掉原先已经打开的数据库连接

                   conn.Connected:= true;
                   HF:= TADOCommand.Create(nil);
                   HF.Connection:= conn;
                   HF.CommandTimeout := 3000;
                   sleep(100);
                   if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
                   sPath:=DirectoryListBox1.Directory+Files
                   Else
                   sPath:=DirectoryListBox1.Directory+'\'+Files;
                   HF.CommandText:= 'Restore DataBase children from disk='''+ sPath + '''';
                   HF.Execute;
                   HF.Free;
                   MessageDlg('数据库恢复完成,现在将重新启动本系统。',mtInformation ,[mbOK],0);
                   self.close;
                   except
                  MessageDlg('数据库恢复失败,请确认没有其它程序已经连接到数据库。现在将重新启动本系统。',mtInformation ,[mbOK],0);

                  end;
                  sPath:=ExtractFilePath(application.ExeName);
                  sFileName:=ExtractFileName(Application.ExeName);
                  sPath:=spath+sFileName;
                  ShellExecute(handle,nil,PChar(sPath),nil,nil,SW_SHOW);
                //  frmmain.Close;
               end;
             end;
          end;
       end;

   if (RadioButton2.Checked=false) and (RadioButton1.Checked=false) then
     begin
       Lab_panel1.Caption:='选择错误,选项备份和恢复不能同时为空';
       Lab_panel1.Font.Color:=clred;
       exit;
     end;

end;

procedure TForm1.ListView1Click(Sender: TObject);

   var Lists:TListItem;
begin
 Lists:=ListView1.Selected;
 if Lists<>nil then  //选择了备份卡
 begin
   Files:=Lists.Caption;
 End
 else Files:='';
 Lab_Panel1.Caption:='选择要恢复的备份';
 Lab_Panel1.Font.Color:=clWindowText;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    form1.Release;
   form1:=nil;
end;

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
  Panel: TStatusPanel; const Rect: TRect);
begin
 Lab_Panel1.Parent:=StatusBar1;
  Lab_Panel1.Left:= Rect.Left+2;
  Lab_Panel1.Height:=14;
  Lab_Panel1.Top:= Rect.Top+2;;
end;

procedure TForm1.DriveComboBox1Change(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;
  Lab_panel1.Caption:='位置选择';
  Lab_panel1.Font.Color:=clWindowText;

end;

procedure TForm1.FormShow(Sender: TObject);
var
List_View:array[0..1]of TListItem;
a:integer;
s:string;
begin
   label2.Caption:='children.bak';      //显示当天的备份信息
   GetDir(3,s);
   //ADOConnection1.Connected:=false;                 //在进行数据库备份时要求中断对数据库的连接和操作
   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;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -