数据库异常状态:
A complete listing of the database errorcodes is found in the
DBIErrs.Int file in the Delphi/Doc directory or in the IDAPI.h
file in the Borland Database Engine.
Database errors are defined by category and code. Here's a sample:
{ ERRCAT_INTEGRITY }
ERRCODE_KEYVIOL = 1; { Key violation }
ERRCODE_MINVALERR = 2; { Min val check failed }
ERRCODE_MAXVALERR = 3; { Max val check failed }
ERRCODE_REQDERR = 4; { Field value required }
ERRCODE_FORIEGNKEYERR = 5; { Master record missing }
ERRCODE_DETAILRECORDSEXIST = 6; { Cannot MODIFY or DELETE this Master record }
ERRCODE_MASTERTBLLEVEL = 7; { Master Table Level is incorrect }
ERRCODE_LOOKUPTABLEERR = 8; { Field value out of lookup tbl range }
ERRCODE_LOOKUPTBLOPENERR = 9; { Lookup Table Open failed }
ERRCODE_DETAILTBLOPENERR = 10; { 0x0a Detail Table Open failed }
ERRCODE_MASTERTBLOPENERR = 11; { 0x0b Master Table Open failed }
ERRCODE_FIELDISBLANK = 12; { 0x0c Field is blank }
The constant for the base category is added to these constants to represent
a unique DBI errorcode;
1、主从表删主表(无级联删除情况)
procedure Tdatamodule1.table1DeleteError(Dataset:Tdataset;E:EDatabaseError;var Action:TdataAction);
begin
if (E is EDBEngineError) then
if (E as EDBEngineError).Error[0].Errorcode=eDetailsExist then
begin
messageDLG('请先删除从表的相关的记录!',mtWarning,[mbOK],0);
abort;
end;
end;
2、主关键字重复
procedure Tdatamodule1.table1PostError(Dataset:Tdataset;E:EDatabaseError;var Action:TdataAction);
begin
if (E is EDBEngineError) then
if (E as EDBEngineError).Error[0].Errorcode=eKeyViol then
begin
messageDLG('主关键字重复!已经存在该编号!',mtWarning,[mbOK],0);
abort;
end;
end;
3、关键字重复和关键字为空的情况
procedure Tdatamodule1.table1PostError(Dataset:Tdataset;E:EDatabaseError;var Action:TdataAction);
var Error:integer;
begin
if (E is EDBEngineError) then
begin
Error:= (E as EDBEngineError).Errors[0].Errorcode;
case Error of
eRequiredFieldMissing: //关键字为空
begin
messageDLG('主关键字不能为空!',mtWarning,[mbOK],0);
Abort;
end;
eKeyViol: //关键字已经存在
begin
messageDLG('主关键字重复!已经存在该编号!',mtWarning,[mbOK],0);
Abort;
end;
end;
end;
end;