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

📄 u_db_restore.~pas

📁 学员管理的软件
💻 ~PAS
字号:
unit U_db_restore;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  inifiles,Dialogs, ComCtrls, Buttons, StdCtrls, ExtCtrls, ADODB, DB,Registry;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    BitBtn1: TBitBtn;
    OpenDialog1: TOpenDialog;
    RadioGroup1: TRadioGroup;
    Label3: TLabel;
    Edit3: TEdit;
    BitBtn2: TBitBtn;
    adoc_create: TADOCommand;
    Label4: TLabel;
    ADOQ_check: TADOQuery;
    ADOConnection1: TADOConnection;
    procedure Button1Click(Sender: TObject);
    procedure createdatabase;
    procedure RadioGroup1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Edit3Enter(Sender: TObject);
    procedure Edit3Exit(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  dbusername,dbuserpass,dbip,dbname:String;

implementation

{$R *.dfm}

procedure Tform1.createdatabase;
var
   Myreg : TRegistry;
   SQL_path : string;
   s_sql : string;
begin
   screen.Cursor := crHourGlass;
   label4.Visible := true;
   label4.Update;
   Myreg := TRegistry.create;
   Myreg.rootkey := HKEY_LOCAL_MACHINE;
   try
      if Myreg.openkey('\SoftWare\Microsoft\MSSQLServer\Setup',False) then
      begin
         SQL_path := myreg.ReadString('SQLPath');
         SQL_path := SQL_path + '\data';
      end;
   finally
      myreg.free;
   end;

   //新建一数据库 exam
   s_sql := '';
   s_sql := s_sql + 'create database '+trim(edit1.Text);
   s_sql := s_sql + ' on (name = '+trim(dbname)+'_data, filename = '''+SQL_path+'\'+trim(dbname)+'_data.mdf'+''') ';
   s_sql := s_sql + 'log on (name = '+trim(dbname)+'_log, filename = '''+SQL_path+'\'+trim(dbname)+'_log.ldf'+''')';
   with ADOC_Create do
   begin
       commandtext := '';
       commandtext := s_sql;
       Execute;
   end;
   //恢复数据库(wifh Replace)
   with ADOC_Create do
   begin
       commandtext := '';
       commandtext := 'Restore database :S_Name from disk=:path with replace';
       parameters.ParamByName('s_Name').value := trim(edit1.Text);
       parameters.ParamByName('path').value := trim(edit2.Text);
       Execute;
   end;
   label3.Visible := false;
   label3.Update;
   screen.Cursor := crDefault;
end;




procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.InitialDir:=extractfilepath(application.ExeName)+'backup_DB\';
if opendialog1.Execute then
edit2.Text:=opendialog1.FileName;
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
case radiogroup1.ItemIndex of
0:
label3.Caption:='服务器名:   ';
1:
begin
label3.Caption:='主机名:   ';
edit3.Text:='127.0.0.1';
end;
end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  IF trim(edit1.Text)='' then
  begin
   application.MessageBox('请填写数据库名称!','错误信息提示',MB_ICONERROR+MB_OK);
   edit1.SetFocus;
   exit;
  end;
  if not fileExists(trim(edit2.text)) then
  begin
      application.MessageBox('没有找到该数据库文件,请核实!','错误信息提示',MB_ICONERROR+MB_OK);
      Edit2.SetFocus;
      exit;
  end
  else
  begin
      if application.messagebox('确定要恢复数据库吗?)','提示信息', MB_OKCANCEL+MB_ICONINFORMATION) <> IDCANCEL then
      begin
          try
              if trim(dbuserpass)='' then
              ADOq_check.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID='+trim(dbusername)+';Initial Catalog=master'
              else
              ADOq_check.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID='+trim(dbusername)+';password='+trim(dbuserpass)+';Initial Catalog=master';
              with ADOq_check do
              begin
                  close;
                  sql.Clear;
                  sql.Add('select name from sysdatabases where name=:sname');
                  parameters.ParamByName('sname').value:=trim(edit1.Text);
                  open;
              end;
          except
              showmessage('连接数据库失败,'+chr(13)+chr(13)+'请核实是否已正确安装 MS SQL Server 2000!');
              exit;
          end; //try
          if ADOQ_check.IsEmpty then createdatabase //恢复数据库
          else
          begin
              ///删除原数据库
              if Application.MessageBox('该操作将删除现有数据库,确认恢复吗?','恢复警告',MB_YESNO+MB_ICONWARNING)=IDYES then
              begin
              if trim(dbuserpass)='' then
              adoc_create.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID='+trim(dbusername)+';Initial Catalog=master'
              else
              adoc_create.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID='+trim(dbusername)+';password='+trim(dbuserpass)+';Initial Catalog=master';
              with   adoc_create do
                 begin
                   commandtext := '';
                   commandtext := 'drop database '+trim(dbname);
                   Execute;
                   createdatabase;
                 end;
              showmessage('数据库已被恢复,请核实!');
              end ;
               exit;
          end; //if

          showmessage('数据库恢复完成!');
      end;//if
  end; //else

end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
s:string;
myfile:Tinifile;
strname,strpass:string;
begin
if (trim(edit3.Text)='') then
begin
  showmessage('请填写完整信息');
  exit;
end;
myfile:=Tinifile.Create(extractfilepath(application.ExeName)+'myfile.ini');
strname:=myfile.ReadString('db_set','name','sa');
strpass:=myfile.ReadString('db_set','pass','');
s:='Provider=SQLOLEDB.1;Persist Security Info=False;';
if trim(strpass)='' then
s:=s+'User ID='+strname+';Initial Catalog=exam;Data Source='+edit3.Text
else
s:=s+'User ID='+strname+';password='+strpass+';Initial Catalog=exam;Data Source='+edit3.Text;
try
showmessage(s);
adoconnection1.ConnectionString:=s;
adoconnection1.Open;
showmessage('已成功连接了数据库');
except
showmessage('服务器连接失败,请重新进行数据库配置');
exit;
end;
end;

procedure TForm1.Edit3Enter(Sender: TObject);
begin
if (sender is Tedit) then
(sender as Tedit).Color:=clInactiveCaptionText;
end;

procedure TForm1.Edit3Exit(Sender: TObject);
begin
if (sender is Tedit) then
(sender as Tedit).Color:=clwindow;
end;

procedure TForm1.FormShow(Sender: TObject);
var
myfile:Tinifile;
begin
myfile:=Tinifile.Create(extractfilepath(application.ExeName)+'myfile.ini');
dbip:=myfile.ReadString('db_set','ipaddress','127.0.0.1');
dbname:=myfile.ReadString('db_set','db_name','exam');
dbusername:=myfile.ReadString('db_set','name','sa');
dbuserpass:=myfile.ReadString('db_set','pass','');
myfile.Free;
end;

end.

⌨️ 快捷键说明

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