英文:
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
的代码可以catch
你throw
的异常,然后可以调用Terminate()
(或Close()
),如文档中所示:
英文:
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论