delphi - 陷阱键违规

标签 delphi delphi-7 ado

嗨,我正在尝试进行一些异常处理并拦截重复的字段值(键违规)错误。在我寻找解决方案的过程中,我看到了许多使用捕获所有错误的建议

try
(enter code)
except on E: EDatabaseError do
showmessage (Error message);
end; 

但我想专门针对关键违规做出响应,它使用使用 ADO 的访问表。

最佳答案

如果您想要处理的唯一错误是带有“重复值”消息的错误,那么这将起作用:

try
  // Your code
except
  on E: EOleException do
  begin
    // The better way is to find out what E.ErrorCode is
    // for this specific exception, and handle it instead
    // of checking the string - you didn't provide the
    // ErrorCode, though.
    // If E.ErrorCode = <whatever> then
    //
    if Pos('duplicate value', E.Message) > 0 then
      // You've got a duplicate with the message above
      // Do whatever handles it
    else
      raise;
  end;
  // If you want to handle other exception types (eg., EDataBaseError),
  // you can do so here:
  //  on E: EDataBaseError do
  //    HandleDBError;
end;

关于delphi - 陷阱键违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165778/

相关文章:

vba - 使用 ADO 执行 LDAP 查询从 Access VBA 检索用户成员资格

Delphi 搜索编辑组件

德尔福-Excel : Define a non-contiguous range

delphi - 重用资源图像位图,而不是在xfm文件中重复它们

sql-server - ADODB 打开记录集失败/"Operation is not allowed when object is closed"

oracle - 从 MSDAORA 提供程序更改为 OraOLEDB 提供程序后,颠倒的问号而不是真实字符

delphi - 编译delphi-pi

delphi - "No Native to Message converter set"是什么意思?

delphi 7 - 创建自定义警告

c# - Delphi 7 Glyph.Data 十六进制到数据 URI