比如在表aa中有字段ename,不能为空,通过dbedit控件输入数据,输入空值。提交的时候会有错误提示,但是我想捕捉这个错误码,实现自己的错误提示信息,当然可以用try..excetp..去做模糊的异常自定义提示,但我想分别根据不同的错误码,相对应显示不同的自定义提示,比如捕捉到关键字空值,就提示自定义空值错误,如果捕捉到类型转换错误就提示自定义类型错误。请问这些不同的错误码怎么获取,谢谢!

解决方案 »

  1.   

    procedure ShowADOErrors(
      con: TADOConnection);
    var
      i: Integer;
      strErrorList: TStrings;
    begin
      if not con.Errors.Count > 0 then
        Exit;  strErrorList := TStringList.Create;
      try
        for i := 0 to con.Errors.Count - 1 do
        begin
          strErrorList.Add(con.Errors.Item[i].Source + ' : ' +
            con.Errors.Item[i].Description + ' ; ' +
            con.Errors.Item[i].SQLState);
        end;
        if trim(strErrorList.Text) = '' then Exit;
        MessageDlg(strErrorList.Text, mtWarning, [mbOK], 0);
      finally
        strErrorList.Free;
      end;
    end;
    //***************************************
    使用:
    try
    except
     showadoerror(con);
    end
      

  2.   


       使用 try  except 啊
      还有 try finally
      

  3.   

    在PostError事件中
    procedure ShowADOErrors(
      con: TADOConnection);
    var
      i: Integer;
      strErrorList: TStrings;
    begin
      if not con.Errors.Count > 0 then
        Exit;  strErrorList := TStringList.Create;
      try
        for i := 0 to con.Errors.Count - 1 do
        begin
          strErrorList.Add(con.Errors.Item[i].Source + ' : ' +
            con.Errors.Item[i].Description + ' ; ' +
            con.Errors.Item[i].SQLState);
        end;
        if trim(strErrorList.Text) = '' then Exit;
        MessageDlg(strErrorList.Text, mtWarning, [mbOK], 0);
      finally
        strErrorList.Free;
      end;
    end;
    //***************************************
    使用:
    try
    except
     showadoerror(con);
    end
      

  4.   

    有个api :getlasterror,可以试试,不过仅限win32系统
      

  5.   

    不行,我这样写:
    procedure TForm1.Button1Click(Sender: TObject);
    var
     i: Integer;
     strErrorList: TStrings;
     begin try
       with adoquery1 do
         begin
           sql.Clear;
           sql.Add('insert into yt (ytname) values(');
           sql.Add('''yt'''+')');
           adoquery1.ExecSQL;
         end;
       except
        begin
         listbox1.Items.Add(adoconnection1.Errors.Item[0].Description);
         listbox1.Items.Add(adoconnection1.Errors.Item[1].Source);
         listbox1.Items.Add(adoconnection1.Errors.Item[2].SQLState);
         listbox1.Items.Add(adoconnection1.Errors.Item[3].HelpFile);
             end;
     end;
    end;
    在listbox中显示:
    违反了primary key约束'pk_yt'.不能在对象'yt'中插入重复键。
    Microsoft OLE DB Provider for SQL Server
    但会弹出窗口:
    项目在所需的名称或序数中未被发现
    请问这个信息怎么捕捉屏蔽?