Inno Setup 通过ADO连接Sql Server并还原数据库

来源:互联网 发布:阿里云ecs购买教程 编辑:程序博客网 时间:2024/06/05 04:36

pascal code:

const

  adCmdUnspecified = $FFFFFFFF;
  adCmdUnknown = $00000008;
  adCmdText = $00000001;
  adCmdTable = $00000002;
  adCmdStoredProc = $00000004;
  adCmdFile = $00000100;
  adCmdTableDirect = $00000200;
  adOptionUnspecified = $FFFFFFFF;
  adAsyncExecute = $00000010;
  adAsyncFetch = $00000020;
  adAsyncFetchNonBlocking = $00000040;
  adExecuteNoRecords = $00000080;
  adExecuteStream = $00000400;
  adExecuteRecord = $00000800;


var
  Page: TWizardPage;
  BtnTestConnect: TNewButton;
  EditDB, EditUser: TNewEdit;
  PasswordEdit: TPasswordEdit;     
  ComboBoxDB: TNewComboBox;     
  StaticTextDB, StaticTextUser, StaticTextPassword : TNewStaticText;
   
function ConnDBAndExecSql(TestConnFlg:Boolean):Boolean;   
var
  ADOCommand: Variant;


  ADOConn: Variant;  


  BakFileName: string;
begin
  Result := False;


  try 
    // create the ADO connection object
    ADOConn := CreateOleObject('ADODB.Connection');
    // build a connection string; for more information, search for ADO
    // connection string on the Internet 
    ADOConn.ConnectionString := 
      'Provider=SQLOLEDB.1;' +               // provider
      'Data Source=localhost;' +   // server name
      'Persist Security Info=True;' +
      'User Id=' + EditUser.Text + ';' +                // user name
      'Password='+ PasswordEdit.Text + ';';                   // password
    // open the connection by the assigned ConnectionString
    ADOConn.Open;
    //ADOConnection.Connected:=True;
  except
    if TestConnFlg then            
      MsgBox('连接失败', mbError, MB_OK)
    else
    begin
      if MsgBox('数据库连接失败,跳过此步?', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = IDYES then
      begin
        Result := True;
      end;
    end; 
    Exit;
  end;


  if TestConnFlg then
  begin
    Result := True;
    MsgBox('连接成功', mbError, MB_OK);   
    Exit;
  end;


  try
    ADOCommand := CreateOleObject('ADODB.Command');
    ADOCommand.ActiveConnection := ADOConn;  
    ADOCommand.CommandText := 'RESTORE DATABASE ceshi11 FROM DISK = ''' + ExpandConstant('{app}') + '\ceshi20161029.bak''' +' WITH REPLACE';  
    //MsgBox( ADOCommand.CommandText, mbInformation, MB_OK);    
    ADOCommand.Execute(NULL, NULL, adCmdText or adExecuteNoRecords);  
    ADOConn.Close; 
    MsgBox( '创建数据库成功', mbInformation, MB_OK);     
                               
  except
    //MsgBox(GetExceptionMessage, mbError, MB_OK); 
    if MsgBox('创建数据库失败,跳过此步?', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = IDYES then
    begin
      Result := True;
    end;
    Exit;
  end;             


  Result := True;
end;


procedure BtnTestConnectOnClick(Sender: TObject);
begin
  ConnDBAndExecSql(True);
end;    






procedure CreateTheWizardPages; 
begin
  { TButton and others }


  Page := CreateCustomPage(wpInstalling, '连接数据库', '数据库设置');


  ComboBoxDB := TNewComboBox.Create(Page);
  ComboBoxDB.Width := Page.SurfaceWidth;
  ComboBoxDB.Parent := Page.Surface;
  ComboBoxDB.Style := csDropDownList;
  ComboBoxDB.Items.Add('Sql Server');
  //ComboBoxDB.Items.Add('Oracle');
  ComboBoxDB.ItemIndex := 0;


  StaticTextDB := TNewStaticText.Create(Page);
  StaticTextDB.Top := ComboBoxDB.Top + ComboBoxDB.Height + ScaleY(16);
  StaticTextDB.Caption := '服务器';
  StaticTextDB.AutoSize := True;
  StaticTextDB.Parent := Page.Surface;


  EditDB := TNewEdit.Create(Page);
  EditDB.Top := StaticTextDB.Top - ScaleY(4);
  EditDB.Left := StaticTextDB.Left + StaticTextDB.Width + ScaleY(8);
  EditDB.Width := Page.SurfaceWidth - EditDB.Left; 
  EditDB.Text := 'localhost';
  EditDB.Parent := Page.Surface;


  StaticTextUser := TNewStaticText.Create(Page);
  StaticTextUser.Top := StaticTextDB.Top + StaticTextDB.Height + ScaleY(16);
  StaticTextUser.Caption := '用户名';
  StaticTextUser.AutoSize := True;
  StaticTextUser.Parent := Page.Surface;


  EditUser := TNewEdit.Create(Page);
  EditUser.Top := StaticTextUser.Top - ScaleY(4);
  EditUser.Left := StaticTextUser.Left + StaticTextUser.Width + ScaleY(8);
  EditUser.Width := Page.SurfaceWidth - EditUser.Left; 
  EditUser.Text := 'sa';
  EditUser.Parent := Page.Surface;       


  StaticTextPassword := TNewStaticText.Create(Page);
  StaticTextPassword.Top := StaticTextUser.Top + StaticTextUser.Height + ScaleY(16);
  StaticTextPassword.Caption := '密码';
  StaticTextPassword.AutoSize := True;
  StaticTextPassword.Parent := Page.Surface;


  PasswordEdit := TPasswordEdit.Create(Page);
  PasswordEdit.Top := StaticTextPassword.Top - ScaleY(4);
  PasswordEdit.Left := EditUser.Left;
  PasswordEdit.Width := Page.SurfaceWidth - PasswordEdit.Left; 
  PasswordEdit.Text := '123456';
  PasswordEdit.Parent := Page.Surface;


  BtnTestConnect := TNewButton.Create(Page);
  BtnTestConnect.Top := StaticTextPassword.Top + StaticTextPassword.Height + ScaleY(16);;
  BtnTestConnect.Width := 100;
  BtnTestConnect.Left :=  (Page.SurfaceWidth - BtnTestConnect.Width) div 2;
  BtnTestConnect.Caption := '测试连接';
  BtnTestConnect.OnClick := @BtnTestConnectOnClick;
  BtnTestConnect.Parent := Page.Surface;                 


end;






function NextButtonClick(CurPageID: Integer): Boolean;
begin  
  Result := False;
  //MsgBox(IntToStr(CurPageID), mbInformation, MB_OK);
  case CurPageID of
    //wpWelcome:
    //wpFinished:
    //wpInstalling:
    10:
      begin
        CreateTheWizardPages; 
      end;
    100:     
      begin
        Result := ConnDBAndExecSql(False);
        Exit;              
      end
    else
    ;   
  end;


  Result := True;
end;
0 0
原创粉丝点击