中止在 OnLogin() 事件中的 TFDConnection 吗?

huangapple go评论58阅读模式
英文:

Abort TFDConnection in OnLogin() event?

问题

我有自己的密码处理程序用于 TFDConnection 对象的 OnLogin 事件。该处理程序的返回值是 void,所以我无法告诉它中止连接并可选择退出程序。

我猜我可能需要以某种形式 throw 一些东西来告诉它中止,但我在文档中没有看到任何内容。

在密码对话框中有一个 Cancel 按钮时,中止连接尝试的正确方式是什么?如果连接失败,我也想退出应用程序。

英文:

I have my own password handler for the OnLogin event of a TFDConnection object. The return value of that handler is void so I can't tell it to abort the connection and optionally exit the program.

I would guess I would then have to throw something in some form to tell it to abort, but I didn't see anything in the documentation.

What is the proper way to abort the attempt to connect in OnLogin when the password dialog has a Cancel button? I'd like to just exit the application if it fails as well.

答案1

得分: 1

I would guess I would then have to throw something in some form to tell it to abort, but I didn't see anything in the documentation.

你的猜测是正确的。只需throw任何你想要的异常。

TFDConnection的默认行为是,当它自己的LoginDialog被取消时,调用FDException()函数来抛出一个EFDException异常,该异常的FDCode属性设置为er_FD_ClntDbLoginAborted

I'd like to just exit the application if it fails as well.

如果失败,你可以在OnLogin处理程序中调用Application->Terminate()(或Application->MainForm->Close())来退出应用程序。或者,连接TFDConnection的代码可以catchthrow的异常,然后可以调用Terminate()(或Close()),如文档中所示:

建立连接(FireDAC):处理连接错误

英文:

> I would guess I would then have to throw something in some form to tell it to abort, but I didn't see anything in the documentation.

Your guess is correct. Simply throw any exception you want.

TFDConnection's default behavior when its own LoginDialog is canceled is to call the FDException() function to throw an EFDException exception that has its FDCode property set to er_FD_ClntDbLoginAborted.

> I'd like to just exit the application if it fails as well.

Before exiting your OnLogin handler, you can call Application->Terminate() (or Application->MainForm->Close()). Or, whatever code is connecting the TFDConnection can catch the exception you throw, and then it can call Terminate() (or Close()), as demonstrated in the documentation:

Establishing Connection (FireDAC) : Handling Connection Errors

huangapple
  • 本文由 发表于 2023年4月17日 07:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定