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

📄 unitbackup.pas

📁 在软件系统中,数据的备份与恢复是相当重要的,下面就给出了一个数据的自动备份程序,DELPHI编写
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  if cboStart.Checked then
    BackupIniFile.WriteInteger('AutoRun','StartMin',1)
  else
    BackupIniFile.WriteInteger('AutoRun','StartMin',0);
  //<备份时显示提示信息>
  if cboShowMessage.Checked then
    BackupIniFile.WriteInteger('AutoRun','Show',1)
  else
    BackupIniFile.WriteInteger('AutoRun','Show',0);
  //<关闭时最小化>
  if cboMin.Checked then
    BackupIniFile.WriteInteger('AutoRun','Min',1)
  else
    BackupIniFile.WriteInteger('AutoRun','Min',0);
  //<显示关闭信息>
  if cboClose.Checked then
    BackupIniFile.WriteInteger('AutoRun','CloseShow',1)
  else
    BackupIniFile.WriteInteger('AutoRun','CloseShow',0);
  Reg.Free;
  ReadIni();
end;

procedure TFrmBackup.btnBackupClick(Sender: TObject);
var
  FilePath:String;
  i:integer;
  str:String;
begin
  try
    if Show=1 then begin
      application.CreateForm(tfrmDlg,frmDlg);
      frmDlg.Show;
    end;

    for i:=0 to cmbDatabaseName.Items.Count-1 do
    begin
      //文件名等于<年月日时分秒>
      BackFileName:=Trim(CmbDatabaseName.Items[i])+FormatDateTime('yyyymmddhhmmss',Now); //取得备份文件名

      //取得地址框中的地址
      if RightStr(EdtPath.text,1)='\' then
         FilePath:=EdtPath.Text
      else
         FilePath:=EdtPath.Text+'\';

      FilePath:=FilePath+BackFileName;
      screen.Cursor:=crSqlWait;

      //      BackupBase(FilePath); //调用备份过程备份文件
      screen.Cursor:=crSqlWait;

      str:='Backup Database '+Trim(CmbDatabaseName.Items[i])+' to Disk='''+FilePath+''' ';

      with connQuery do
      begin
        close;
        Sql.Text:=str;
        ExecSQL;
      end;
    end;

    screen.Cursor:=crDefault;

    if Show=1 then
    begin
      frmDlg.Close;
      frmDlg.Free;
    end;
    
  except
    MessageBox(handle,'备份失败!','提示',MB_IconWarning+mb_Ok);
    screen.Cursor:=crDefault;
    frmDlg.Close;
    frmDlg.Free;
    Exit;
  end;
end;


procedure TFrmBackup.btnPathClick(Sender: TObject);
begin
  Application.CreateForm(TFrmDirPath,FrmDirPath); 
  if FrmDirPath.ShowModal=mrOk then
    EdtPath.Text:=FrmDirPath.DirectoryListBox1.Directory;
end;

procedure TFrmBackup.Timer1Timer(Sender: TObject);
var
  Year,Month,Day,Hour,Min,Sec,MSec:Word;
begin
  DecodeDate(Now,Year,Month,Day);
  DecodeTime(Now, Hour, Min, Sec, MSec);

  case BackupType of
  0:begin//不备份
      //
    end;
  1:begin//每小时
      if (Min=EveryTime) and (Sec=0) then
        btnBackupClick(btnBackup);
    end;
  2:begin//每天
      if (Hour=EveryDay) and (Min=EveryTime) and (Sec=0) then
        btnBackupClick(btnBackup);
    end;
  3:begin//每周
      if ((DayOfWeek(Date)-1)=EveryWeek) and (Hour=EveryDay) and (Min=EveryTime) and (Sec=0) then
        btnBackupClick(btnBackup);
    end;
  4:begin//每月
      if (Day=EveryMonth) and (Hour=EveryDay) and (Min=EveryTime) and (Sec=0) then
        btnBackupClick(btnBackup);
    end;
  end;

end;

procedure TFrmBackup.FormCreate(Sender: TObject);
begin
  ReadIni();
  connString:='Driver={SQL server};server='+Trim(CmbServerName.Text)+';database='+Trim(CmbDatabaseName.Text)+
              ';uid='+Trim(EdtUserName.Text)+';pwd='+Trim(EdtPassword.Text);
  try
    connAdo.ConnectionString:=connString;
    connAdo.LoginPrompt:=False;
    connAdo.Connected:=True;
    conn:=True;
  except
    MessageBox(handle,'联接数据库错误','错误',mb_IconWarning+mb_Ok);
    Exit;
  end;


  //判断启动时是否最小化窗体
 { if StartMin=1 then
  begin
    //Application.Minimize;
    AddIcon(handle);
  end;  }
end;

procedure TFrmBackup.btnCancelClick(Sender: TObject);
begin
  //取消所做的修改
  ReadIni();
end;

procedure TFrmBackup.N2Click(Sender: TObject);
begin
  close;
end;

procedure TFrmBackup.N1Click(Sender: TObject);
begin
  FrmBackup.Visible := True;
end;

procedure TFrmBackup.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if CloseShow=1 then
  begin
    if MessageBox(Handle,'确实要关闭系统吗?','提示',MB_YESNO+MB_ICONINFORMATION)=IDNO then
    begin
      canClose:=False;
    end
    else
    begin
      RemoveIcon(handle);
      Application.Terminate ;
    end;
  end
  else begin
    RemoveIcon(handle);
    Application.Terminate ;
  end;
end;

procedure TFrmBackup.BitBtn1Click(Sender: TObject);
begin
  if Trim(CmbServerName.Text)='' then
  begin
    MessageBox(handle,'数据库服务器名称不能为空!','错误',mb_IconWarning+mb_Ok);
    CmbServerName.SetFocus;
    Exit;
  end;
  if Trim(CmbDatabaseName.Text)='' then
  begin
    MessageBox(handle,'数据库名称不能为空!','错误',mb_IconWarning+mb_Ok);
    CmbDatabaseName.SetFocus;
    Exit;
  end;
  if Trim(EdtUserName.Text)='' then
  begin
    MessageBox(handle,'用户名不能为空!','错误',mb_IconWarning+mb_Ok);
    EdtUserName.SetFocus;
    Exit;
  end;
  connString:='Driver={SQL server};server='+Trim(CmbServerName.Text)+';database='+Trim(CmbDatabaseName.Text)+
              ';uid='+Trim(EdtUserName.Text)+';pwd='+Trim(EdtPassword.Text);
  try
    screen.Cursor:=crSqlwait;
    with connAdo do
    begin
      if Connected then
         Connected:=False;
      ConnectionString:=connString;
      LoginPrompt:=False;
      Connected:=True;
    end;
    MessageBox(handle,'联接成功!','提示',MB_ICONINFORMATION+mb_Ok);
    conn:=True;
    screen.Cursor:=crdefault;
  except
    MessageBox(handle,'联接失败!','提示',mb_IconWarning+mb_Ok);
    screen.Cursor:=crdefault;
    conn:=False;
    Exit;
  end;
end;

procedure TFrmBackup.Button1Click(Sender: TObject);
var
  i,j:integer;
begin
  Application.CreateForm(TfrmChoose,frmChoose);
  //取得服务器中所有数据库名
  with connQuery do
  begin
    Close;
    Sql.Text:='select * from master.dbo.sysdatabases';
    open;
    while not Eof do
    begin
      frmChoose.list.Items.Add(Fields[0].Value);
      next;
    end;
  end;

  //列表框中取得要备份的数据库名称
  for i:=0 to cmbDatabaseName.Items.Count-1 do
  begin
    for j:=0 to frmChoose.list.Count-1 do
    begin
      if frmChoose.list.Items[j]=cmbDatabaseName.Items[i] then
        frmChoose.list.Checked[j]:=True;  
    end;
  end;

  if frmChoose.ShowModal=mrok then
  begin
    cmbDatabaseName.Clear;
    DatabaseName:='';
    for i:=0 to frmChoose.list.Count-1 do
    begin
      if frmChoose.list.Checked[i] then
      begin
        DatabaseName:=DatabaseName+frmChoose.list.Items[i]+'|';
        cmbDatabaseName.Items.Add(frmChoose.list.Items[i]);
      end;
    end;

    cmbDatabaseName.ItemIndex:=0;
  end;
end;


procedure TFrmBackup.ComBoboxValue(Cmb:TComBoBox;Str:string) ;
var i,j,k,n:integer ;
    s:string;
begin
  K:=0 ; n:=1;
  Cmb.Clear;
  For i:=1 to Length(Str) do
    if copy(Str,i,1)='|' then
      begin
        s:=copy(Str,n,i-n) ;
        Cmb.Items.Add(s);
        n:=i+1;
        k:=K+1 ;
      end ;
     Cmb.ItemIndex:=0; 
end;

end.

⌨️ 快捷键说明

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