下面一个函数是用来从一个文本文件中读取数据,采用了缓存模式
但是问题在出异常了以后还是将记录给提交了,
我就想在发生异常得时候一条记录都不提交应该如何修改??
function  TFormsjzr.sjzr(filename:string;Tstr:tstringList;table:ToraTable;len:integer;sep:string;Tip:string):boolean;
 var
 j:integer;
 str:string;
 postion:integer;
 myexception: exception;  begin
   
    if (i < Tstr.Count) and (copy(Tstr.Strings[i],
      length(Tstr.Strings[i]) - len, len+1) = sep) then
    begin
      table.close;
      table.Prepare ;
      table.open;
    end;
     
    while (i < Tstr.Count) and (copy(Tstr.Strings[i],length(Tstr.Strings[i]) - len, len+1) = sep) do
    begin
    try
        j := 0;
        table.insert;
        str := Tstr.Strings[i];
        while (j < table.fieldcount - 1) do
        begin
          postion := pos(',', str);
          if j = table.fieldcount - 2 then
          begin
            if postion <> 1 then
              table.fields[j].value := copy(str, 1, postion - 1)
            else
              table.fields[j].value := null;
          end
          else if postion <> 1 then
            table.fields[j].value := copy(str, 1, postion - 1)
          else
            table.fields[j].value := null;
          str := copy(str, postion + 1, (length(str) - postion));
          j := j + 1;
        end; //end while
        table.post;
        i := i + 1;
        StatusBarmsg.Panels[0].text :='正在处理:'+FileName;
        StatusBarmsg.Panels[1].text := '已处理' + inttostr(i) + '条记录,总共有'
          + inttostr(Tstr.Count) + '条记录';
        FormMain.RxTrayIcon1.Hint:=Tip+inttostr(i)+'条记录,总共有'+inttostr(Tstr.Count) + '条记录';
        FormMain.Refresh ;
        Formsjzr.Refresh;
      except
            on e:exception do
            begin
              edtbaodan.Lines.add(pchar('错误原因如下:'+chr(13)+convert_error_message(e.message)));
              edtbaodan.Lines.Add('文件:'+FileName+Tstr.Strings[i]+'该条报单记录出错,请重新核对!');
              table.CancelUpdates ;
              i := i + 1;
              transflag:=true;
              continue;           end;
      end;
      table.ApplyUpdates ;
    end;
  end;

解决方案 »

  1.   

    on e:exception do
                begin
                  edtbaodan.Lines.add(pchar('错误原因如下:'+chr(13)+convert_error_message(e.message)));
                  edtbaodan.Lines.Add('文件:'+FileName+Tstr.Strings[i]+'该条报单记录出错,请重新核对!');
                  table.CancelUpdates ;
                  i := i + 1;
                  transflag:=true;
                  continue;
                  Exit;//////------------------------此处退出
               end;
      

  2.   

    楼上得兄弟,按照你说得改成break不就可以了
    但是还是在数据库中提交了啊
    我得目的就是数据库中得数据回滚啊
    我用得是ORACLE9
      

  3.   

    如果用adoconnection控件连接数据库的话;你可以这样;  if adoconnection1.InTransaction then
          adoconnection1.RollbackTrans;
      adoconnection1.BeginTrans;
      try
        ...    adoconnection1.CommitTrans;
      except
        adoconnection1.RollbackTrans;
      end;
      

  4.   

    问题是我用得是ODAC直接连接ORACLE啊
    不是ODBC
    好像那个
    table.CancelUpdates ;
    没有作用一样啊