Delphi, FireDAC and custom login sequence


Using FireDac with SQL Server. For a program where I manage a databasconnection (and thus not always has a database to connect to from the beginning), I want this to happen:

將FireDac與SQL Server一起使用。對於我管理數據庫連接的程序(因此並不總是有一個數據庫從頭開始連接),我想要這樣做:

  1. The user connects and the OnLogin event is triggered.
  2. 用戶連接並觸發OnLogin事件。
  3. First I want to try default values/values from a settings file
  4. 首先,我想嘗試設置文件中的默認值/值
  5. If that fails due to wrong user_name/password I want to show a password dialog
  6. 如果由於錯誤的user_name / password而失敗,我想顯示密碼對話框
  7. Repeat step 3. until login ok or user cancels.
  8. 重復步驟3.直到登錄確定或用戶取消。

There is some handling of a similar scenario in FireDacs own logindialog, but it seems very complex and the dialog is ugly (I would lika my own!), and also doesn't have a checkbox for "Use OS authentication" that I would need.

在FireDacs自己的logindialog中有一些類似場景的處理,但它看起來非常復雜,對話框很難看(我會自己做的!),也沒有“使用OS身份驗證”的復選框,我需要它。

Any hints would be great!

任何提示都會很棒!

/Anders

/安德斯

1 个解决方案

#1


0  

If you don't want to make a TFDGUIxLoginDialog dialog descendant, you can simply write a loop in which you'll be trying to open the connection handling EFDDBEngineException exception, and asking its Kind for ekUserPwdInvalid (optionally ekUserPwdExpired, or ekUserPwdWillExpire when you need to). Without the OnLogin event handler it can be something like this:

如果您不想創建TFDGUIxLoginDialog對話后代,您可以簡單地編寫一個循環,在該循環中您將嘗試打開處理EFDDBEngineException異常的連接,並在需要時詢問其Kind用於ekUserPwdInvalid(可選擇ekUserPwdExpired或ekUserPwdWillExpire) )。沒有OnLogin事件處理程序,它可以是這樣的:

procedure TForm1.Button1Click(Sender: TObject);
var
  LoginDialog: TMyLoginDialog;
begin
  { ← assign parameters from config file here }
  while True do
  try
    FDConnection.Open; { ← try to connect }
  except
    on E: EFDDBEngineException do
      case E.Kind of
        ekUserPwdInvalid: { ← here you can show your login dialog }
        begin
          LoginDialog := TMyLoginDialog.Create(nil);
          try
            if LoginDialog.ShowModal = mrOk then
            begin
              { ← assign parameters from login dialog here }
            end
            else
              raise; { ← user cancelled login dialog }
          finally
            LoginDialog.Free;
          end;
        end;
        ekUserPwdExpired: { here you can show password change dialog };
        ekUserPwdWillExpire: { here you can optionally show password change dialog };
      else
        raise;
      end;
    else
      raise;
  end;
end;

The OnError event is no good for this task because there's no way to eat the exception from there. If you release the exception object there (to pretend login was successful and let the engine fail on something else for reconnect attempt), you may get unpredicted behavior (so as when you modify the exception object in a bad way).

OnError事件對此任務沒有好處,因為沒有辦法從那里吃掉異常。如果你在那里釋放異常對象(假裝登錄成功並讓引擎在其他地方失敗以進行重新連接嘗試),你可能會得到不可預測的行為(所以當你以一種糟糕的方式修改異常對象時)。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com