数据库的备份与还原+INI文件数据库参数配置

 利用SQL语句来完成对SQL Server数据库的备份与还原功能 仅以练习

数据库的备份与还原+INI文件数据库参数配置_第1张图片

 

完成代码如下:

------------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    GroupBox1: TGroupBox;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Bevel1: TBevel;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses unit2,unit3,unit4;

 

procedure TForm1.Button1Click(Sender: TObject);
var
  str:string;
begin
  str:='select number as 编号,name as 姓名,tel as 联系电话 from info';
  AdoQuery1.Close ;
  AdoQuery1.SQL.Clear ;
  AdoQuery1.SQL.Add(str);
  AdoQuery1.Open ;
end;

 

procedure TForm1.Button2Click(Sender: TObject);
begin
  form2:=Tform2.Create(form1);
  form2.Show ;
  ADOQuery1.Edit ;
  form2.Edit2.Text :=ADOQuery1.FieldByName('编号').AsString ;
  form2.edit3.Text :=ADOQuery1.FieldByName('姓名').AsString ;
  form2.edit4.Text :=ADOQuery1.FieldByName('联系电话').AsString ;
end;

 

//数据库的INI文件配置(该模块代码见下----)

procedure TForm1.Button3Click(Sender: TObject);
begin
  CfgForm:=TCfgForm.Create(form1);
  CfgForm.Show ;
end;

 

procedure TForm1.Button4Click(Sender: TObject);
var
  ss,sd:string;
begin
  sd:=extractfilepath(paramstr(0))+'backup/'+datetostr(now)+'.mdf';
  //SaveDialog1.Execute ;
  ss:='backup database test to disk='''+sd+'''';
  //showmessage(sd);
  if fileexists(sd) then
    begin
    if messagebox(0,' 今天备份文件已存在!是否覆盖?','警告',MB_OKCANCEL)= IDCANCEL then
      exit
    else
      DeleteFile(sd);
      ADOQuery1.Close ;
      ADOQuery1.SQL.Clear ;
      ADOQuery1.SQL.Add(ss);
      ADOQuery1.ExecSQL ;
      showmessage('数据库备份完成!'); ;
      exit;
    end
  else
    begin
    ADOQuery1.Close ;
    ADOQuery1.SQL.Clear ;
    ADOQuery1.SQL.Add(ss);
    ADOQuery1.ExecSQL ;
    showmessage('数据库备份完成!');
    end;
  {st:=SaveDialog1.FileName ;
  ss:='backup database test to disk='''+st+'.mdf''';
  if fileexists(st+'.mdf') then
    if Messagebox(0,'该文件已存在!,您是否要覆盖该文件?','警告',MB_OKCANCEL)=IDCANCEL then
       exit
    else
      begin
      deletefile(st+'.mdf');
      ADOQuery1.Close ;
      ADOQuery1.SQL.Clear ;
      ADOQuery1.SQL.Add(ss);
      ADOQuery1.ExecSQL ;
      showmessage('数据库备份完成!');
      end
  else
  begin
    //showmessage(ss);
    //DataModule4.ADOConnection1.ConnectionString :='';
    ADOQuery1.Close ;
    ADOQuery1.SQL.Clear ;
    ADOQuery1.SQL.Add(ss);
    ADOQuery1.ExecSQL ;
    showmessage('数据库备份完成!');
  end;}
end;

 

procedure TForm1.Button5Click(Sender: TObject);
var
  ss,sql:string;
begin
  SaveDialog1.Execute ;
  ss:=SaveDialog1.FileName ;
  sql:='use master restore database test from disk='''+ss+'''';
  if messagebox(0,' 您确认要执行恢复操作吗?','警告',MB_OKCANCEL)= IDCANCEL then
     exit
  else
  begin
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear ;
  ADOQuery1.SQL.Add(sql);
  ADOQuery1.ExecSQL ;
  showmessage('数据库恢复完成!');
  end;
end;

 

procedure TForm1.Button6Click(Sender: TObject); //获取当前路径
begin
  //GetModuleFileName();
  ShowMessage(extractfilepath(application.exename));
  ShowMessage(extractfilepath(paramstr(0)));
end;

end.

---------------------------------

数据库的备份与还原+INI文件数据库参数配置_第2张图片

数据库实现INI文件配置 完整代码:

---------------------------------

unit Unit3;

interface

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

type
  TCfgForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  CfgForm: TCfgForm;

implementation
var
  myinifile:Tinifile;
{$R *.dfm}

 

procedure TCfgForm.Button2Click(Sender: TObject);
begin
  CfgForm.Free ;
end;

 

procedure TCfgForm.FormCreate(Sender: TObject);
var
  filename:string;
begin
  filename:=ExtractFilePath(paramstr(0))+'SQLcfg.ini';
  myinifile:=Tinifile.Create(filename);
  edit1.Text :=myinifile.ReadString('参数配置','Data Source','');
  edit2.Text :=myinifile.ReadString('参数配置','Initial Catalog','');
  edit3.Text :=myinifile.ReadString('参数配置','User ID','');
  edit4.Text :=myinifile.ReadString('参数配置','Password','');
end;

 

procedure TCfgForm.Button1Click(Sender: TObject);
begin
  myinifile.WriteString('参数配置','Data Source',edit1.text);
  myinifile.WriteString('参数配置','Initial Catalog',edit2.text);
  myinifile.WriteString('参数配置','User ID',edit3.text);
  myinifile.WriteString('参数配置','Password',edit4.text);
  myinifile.Destroy ;
  CfgForm.Release ;
end;

 

procedure TCfgForm.Button3Click(Sender: TObject);
begin
  showmessage(inttostr(CfgForm.InstanceSize));
end;

end.
-------------------------------

SQLcfg.ini 文件内容:

---------------

[参数配置]
Data Source=BULY
Initial Catalog=test
User ID=sa
Password=1001

---------------

2009-11-04

你可能感兴趣的:(数据库的备份与还原+INI文件数据库参数配置)