program project1 uses forms,windows,classes,dialogs,db,,dbtables,sysutils, unit1 in ’unit1.pas’ {form1};
type tmyclass=class(tobject) public procedure AppException(Sender :TObject; E:Exception); procedure DBEngineError( E :EDBEngineError ); procedure DatabaseError( E :EDatabaseError ); function strkeyviol(str:string):string; function strtranengconstriant(str:string):string; end; const maxCon_straint=1; con_e=0 con_c=1 //================ // 数据库错误处理 //================
procedure Tmyclass.AppException(Sender :TObject; E:Exception); begin if (E is EDBEngineError) then begin if ( E as EDBEngineError ).ErrorCount>1 then DBEngineError( E as EDBEngineError ) else Application.ShowException(E); end else if (E is EDatabaseError) then begin DatabaseError( E as EDatabaseError ); end else Application.ShowException(E); end;
//对于服务器错误,处理Errors[0,1]
procedure Tmyclass.DBEngineError( E :EDBEngineError ); const errcode_keyviol:tdberror=1; begin case E.Errors[0].SubCode of ERRCODE_KEYVIOL: // Primary Key, Unique Index //11111111111 ShowError( strKeyViol(E.Errors[1].Message) ); else{case} Application.ShowException(E); end;{case} end;
//处理本地发现的错误 procedure tmyclass.DatabaseError( E :EDatabaseError ); begin if Pos(’must have a value’, E.Message)<>0 then ShowError(’”’ + strGetToken( E.Message, ’’’’ ) + ’”不能为空’ ) else if Pos(’ not a valid float’,E.Message)<>0 then begin ShowErr or(’您输入的数字不合法’) end else Application.ShowException(E); end;
function Twyclass.strKeyViol( str: string ): string; var strE: string; begin {$IFDEF DBMS_SQL65} strE := strGetToken( str, ’’’’ ); Result := ’”’ + strTranEngConstraint(strE) + ’”不能重复’; {$ELSE IFDEF DBMS_INTER5} strE := strGetToken( str, ’”’ ); Result := ’”’ + strTranEngConstraint(strE) + ’”不能重复’; {$ENDIF} end;
function Tmyclass.strTranEngConstraint( str: string ): string; var i: integer; isFound: boolean; begin isFound := False; for i := 0 to maxConstraint do if a2strConstraint[i,conE] = str then begin isFound := True; Break; end;
if isFound then Result := a2strConstraint[i,conC] else Result := ’未知错误(’ + str + ’)’; end;