简体   繁体   English

Delphi,FireDAC和自定义登录顺序

[英]Delphi, FireDAC and custom login sequence

Using FireDac with SQL Server. 在SQL Server中使用FireDac。 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: 对于我管理数据库连接(因此从一开始就不总是要有数据库连接)的程序,我希望发生这种情况:

  1. The user connects and the OnLogin event is triggered. 用户连接并触发了OnLogin事件。
  2. First I want to try default values/values from a settings file 首先,我想尝试设置文件中的默认值/值
  3. If that fails due to wrong user_name/password I want to show a password dialog 如果由于错误的用户名/密码失败,我想显示一个密码对话框
  4. Repeat step 3. until login ok or user cancels. 重复步骤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自己的登录对话框中有一些类似情况的处理方法,但是它看起来非常复杂并且对话框很丑陋(我会用我自己的!),并且也没有我需要的“使用OS身份验证”复选框。

Any hints would be great! 任何提示都很棒!

/Anders /安德斯

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). 如果您不想成为TFDGUIxLoginDialog对话框的后代,则只需编写一个循环,尝试打开连接处理EFDDBEngineException异常,并向其Kind请求ekUserPwdInvalid (可选的ekUserPwdExpiredekUserPwdWillExpire,当您需要)。 Without the OnLogin event handler it can be something like this: 如果没有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. OnError事件不利于此任务,因为无法从那里吞噬异常。 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 ). 如果在该处释放异常对象(以假装登录成功并让引擎在其他情况下失败以进行重新连接尝试),则可能会遇到无法预料的行为(例如,当您以不良方式修改异常对象时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM