const
eKeyViol = 9729;
在table的onposterror事件中写如下代码
procedure TDM.CustomerPostError(DataSet: TDataSet;
E: EDatabaseError; var Action: TDataAction);
begin
if (E is EDBEngineError) then
if (E as EDBEngineError).Errors[0].Errorcode = eKeyViol then
begin
MessageDlg('Unable to post: Duplicate Customer ID.', mtWarning, [mbOK], 0);
Abort;
end;
end;
2:
如果错误信息不能用前面的方法捕获,可惜如下代码实现
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnException := AppException;
end;
procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
showmessage('关键字不能重复');
abort;
end;
procedure AppException(Sender: TObject; E: Exception);
必须定义载创体类中