简单问题:拦截数据库发出的出错消息(100分)

P

panjf

Unregistered / Unconfirmed
GUEST, unregistred user!
我用dbgrid显示和编辑数据库里的数据,当记录指针变化就post当前记录,这是如果这条
记录不符合数据库的某些约定(如不为空的为空了),数据库就会出发一个异常,请问这
个异常在什么控件的什么事件里捕捉?
 
beforeinsert
 
OnpostError
 
DataSet.onpost里嘛
 
可以在 BeforePost 中进行校验,如果不满足条件,
就不提交
 
例如:主工程文件中,此代码经检验过了
procedure TMAIN_Form.myExceptHandle(Sender : TObject; E : Exception);
var
DestStr:pChar;
begin
if Pos('valid date',E.Message) > 0 then //捕获非法日期
Application.messagebox(' 输入了非法日期!',pchar(application.title),mb_iconstop+mb_ok)
else if Pos('Invalid input value',E.Message) > 0 then //非法输入
Application.messagebox(' 非法输入!用ESC放弃操作 ',pchar(application.title),mb_iconstop+mb_ok)
else if (Pos('Master record missing',E.Message) > 0)then //非法输入
Application.messagebox(' 删除错误!检查此记录是否被引用 ',pchar(application.title),mb_iconstop+mb_ok)
else if (Pos('Cannot insert the value NULL',E.Message) > 0)then //非法输入
Application.messagebox(' 不能录入空值! ',pchar(application.title),mb_iconstop+mb_ok)
else if (Pos('Value out of bounds',E.Message) > 0)then //非法输入
Application.messagebox(' 输入值超过限定界限! ',pchar(application.title),mb_iconstop+mb_ok)
end;
procedure TMAIN_Form.FormCreate(Sender: TObject);

var
lBufSize :Cardinal;
lStatus :LongBool;
sBuffer:pChar;
begin
Application.OnException := myExceptHandle;
//异常处理
end;
 
to thebest:
如果判断到了不符合条件,如何终止Post的执行?
 
95. Delphi中获得BDE、ADO的错误号
Delphi的数据库对象,如Ttable和TadoTable有以下一些Error事件:OnDeleteError、OnDeleteErro、OnPostError。这些事件的定义如下,都是数据集错误:
type TDataSetErrorEvent = procedure(DataSet: TDataSet; E: EDatabaseError; var Action: TDataAction) of object;
property OnPostError: TDataSetErrorEvent;

在这些事件在EdatabaseError中是无法得到错误号的,其中只有Message属性。在发生BDE错误时可如下得到错误号:
if E is EDBEngineError then
showmessage(inttostr(EDBEngineError(E).Errors[0].ErrorCode));
也可以这么写:
if E is EDBEngineError then
showmessage(inttostr((E As EDBEngineError).Errors[0].ErrorCode));
但在发生ADO错误时不能这样: E is EadoError。
其实Ado错误在数据集相应的数据库连接中都有:TadoConnection.Errors。
procedure TForm1.Table1PostError(DataSet: TDataSet; E: EDatabaseError;
var Action: TDataAction);
var
i:integer;
begin
memo1.Lines.Add(inttostr(table1.Connection.errors.count ));
for i:=0 to AdoConnection1.errors.count-1 do
begin
memo1.Lines.Add('number:'+inttohex(AdoConnection1.errors.Number,8 ));
memo1.Lines.Add('NativeCode:'+inttostr(AdoConnection1.errors.NativeError ));
memo1.Lines.Add(inttostr(AdoConnection1.errors.HelpContext ));
memo1.Lines.Add(AdoConnection1.errors.Source );
memo1.Lines.Add(AdoConnection1.errors.SQLState );
memo1.Lines.Add('Description:'+AdoConnection1.errors.Description );
end;
memo1.Lines.Add('error Msg :'+e.message);
end;
NativeError是Foxpro的原生错误号,非常详细,一般用这个来判断发生的错误。Number是Sql错误号,是大的分类,一般一个Number和多个NativeError对应。
这些错误码的信息在文档Drvvfp.hlp中都有。

 
BeforePost 中进行校验 错误时 abort
onposterror 给出自己的提示,然后abort
 
多人接受答案了。
 
顶部