SQLSERVER数据库备份/恢复

时间:2022-09-25 07:58:49

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit; //IP输入框
    Edit2: TEdit; //数据库用户名
    Edit3: TEdit; //密码
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    Label4: TLabel;
    ComboBox1: TComboBox;//选择要操作的数据库下拉框
    Label5: TLabel;
    Edit4: TEdit;
    Button1: TButton;//备份
    Button2: TButton;//恢复
    dbcon: TADOConnection;
    Button3: TButton;//连接数据库
    DBRS: TADOQuery;
    SaveDialog1: TSaveDialog;
    OpenDialog1: TOpenDialog;
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Edit1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//登录数据

procedure TForm1.Button3Click(Sender: TObject);
begin
  if dbcon.Connected then
  begin
    Application.MessageBox(PCHAR('服务器已连接,请执行下一步!'),PCHAR(self.caption),MB_OK+MB_ICONERROR);
    exit;
  end;
  if trim(edit1.Text)='' then
  begin
    Application.MessageBox(PCHAR('请输入服务器地址(IP或机器名)!'),PCHAR(self.caption),MB_OK+MB_ICONINFORMATION);
    exit;
  end;
  if trim(edit2.Text)='' then
  begin
    Application.MessageBox(PCHAR('请输入登录用户名!'),PCHAR(self.caption),MB_OK+MB_ICONINFORMATION);
    exit;
  end;
  dbcon.ConnectionString:='Provider=SQLOLEDB.1;Password='+edit3.Text +
       ';Persist Security Info=True;User ID='+trim(edit2.Text )+
       ';Initial Catalog=master;Data Source='+trim(edit1.Text );
  try
    dbcon.open;
    dbrs.SQL.Text :='select  name   from   sysdatabases where dbid>5 order by name';
    dbrs.Open ;
    while not dbrs.Eof do
    begin
      combobox1.Items.Add(dbrs.fieldbyname('name').AsString );
      dbrs.Next;
    end;
    dbrs.Close;
    Application.MessageBox(PCHAR('连接成功,请选择待备份或恢复的数据库!!'),PCHAR(self.caption),MB_OK+MB_ICONINFORMATION);
  except
    Application.MessageBox(PCHAR('连接失败!'),PCHAR(self.caption),MB_OK+MB_ICONERROR);
  end;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  dbrs.Close;
  dbcon.Close;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  dbcon.Close;
  combobox1.Items.Clear ;
end;

//备份功能

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not dbcon.Connected then
  begin
    Application.MessageBox(PCHAR('服务器尚未连接,不能执行备份!'),PCHAR(self.caption),MB_OK+MB_ICONWARNING);
    exit;
  end;
  if combobox1.Text ='' then
  begin
    Application.MessageBox(PCHAR('请选择待备份的数据库!'),PCHAR(self.caption),MB_OK+MB_ICONWARNING);
    exit;
  end;
  SaveDialog1.FileName :=combobox1.Text+formatdatetime('_YYYYMMDD_HHMMSS',now);
  if not SaveDialog1.Execute then exit;
  edit4.Text :=SaveDialog1.FileName ;
  try
    dbcon.Execute('backup database '+combobox1.text+' to disk='+''''+edit4.Text+'''' );
    Application.MessageBox(PCHAR('备份成功!'),PCHAR(self.caption),MB_OK+MB_ICONINFORMATION);
  except
    Application.MessageBox(PCHAR('备份失败!'),PCHAR(self.caption),MB_OK+MB_ICONERROR);
  end;
end;

//恢复功能

procedure TForm1.Button2Click(Sender: TObject);
begin
  if not dbcon.Connected then
  begin
    Application.MessageBox(PCHAR('服务器尚未连接,不能执行恢复!'),PCHAR(self.caption),MB_OK+MB_ICONWARNING);
    exit;
  end;
  if combobox1.Text ='' then
  begin
    Application.MessageBox(PCHAR('请选择待恢复的数据库!'),PCHAR(self.caption),MB_OK+MB_ICONWARNING);
    exit;
  end;
  if not OpenDialog1.Execute then exit;
  edit4.Text :=OpenDialog1.filename;
  try
    dbcon.Execute('restore database '+combobox1.text+' from disk='+''''+edit4.Text+'''' );
    Application.MessageBox(PCHAR('恢复成功!'),PCHAR(self.caption),MB_OK+MB_ICONINFORMATION);
  except
    Application.MessageBox(PCHAR('恢复失败!'),PCHAR(self.caption),MB_OK+MB_ICONERROR);
  end;

end;

end.