我有一个循环吧记录插入数据库,数据表我设了关键字,当插入重复记录时,产生错误,我想当插入重复记录时继续执行下一条语句,就像VB中的on error resume next
,如何实现。
我是这样:
        try
          Cn.BeginTrans;
          while StrTemp <>Chr(4) do
Label1:
          begin
            //其中每一次Strtemp1,StrTemp2.....的值都不同
            Cn.Execute('Insert Into tCMS_Consume_Dtl(PhyCardID,decBalance,decConsume,datTime,intPosID,VarServer)' +
              ' Values(''' + StrTemp1 + ''',' + StrTemp2 + ',' + StrTemp3 + ',''' +
              StrTemp4 + ''',' +StrTemp5 + ',''' + 'dd' + ''')');
            //其它操作
            ...
          end;
          Cn.CommitTrans;
        except
          on E:EOleException do
          begin
            Case E.ErrorCode of
              -2147217873://重复记录
                   GoTo Label1;
              else
                Cn.RollbackTrans;
            end;
          end;
        end;
编译出现:
goto label1 leads into or out of 'try' statement
怎么回事

解决方案 »

  1.   

    delphi没有on error resume next这个功能
      

  2.   

    加if table.locate('关键字段名',StrTemp1,[])=true then
    continue
      

  3.   

    在try中goto是不允许的。你可以将Label1中的代码写到一个过程中,如
    procedure OneError;
    begin
      //……
    end;
    将GoTo Label1;改为调用OneError。————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————
      

  4.   

    To  lxpbuaa(桂枝香在故国晚秋):
    关键是我想goto的地方是一个循环体,假如把它写成一个过程,一个循环执行了几条,某一条数据发生错误如何让他继续执行下去,我要解决的内容是这样:
    我有一个循环吧记录插入数据库,数据表我设了关键字,当插入重复记录时,产生错误,我想当插入重复记录时继续执行下一条语句,就像VB中的on error resume next
      

  5.   

    那这样就可以了:
    try
        Cn.BeginTrans;
        while StrTemp <>Chr(4) do
        try
          //其中每一次Strtemp1,StrTemp2.....的值都不同
                Cn.Execute('Insert Into tCMS_Consume_Dtl(PhyCardID,decBalance,decConsume,datTime,intPosID,VarServer)' +
                  ' Values(''' + StrTemp1 + ''',' + StrTemp2 + ',' + StrTemp3 + ',''' +
                  StrTemp4 + ''',' +StrTemp5 + ',''' + 'dd' + ''')');
                //其它操作
        except
          on E:EOleException do
            if E.ErrorCode = -2147217873 then Continue;
        end;
        Cn.CommitTrans;
      except
        Cn.RollbackTrans;
      end;————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————