procedure TxtToDB(SourceTable:TAdotable;Txtname:TFileName;fdcount:integer); //文本入数据库
var
  F1:Textfile;
  i:integer;
  TmpStr:string;
  ch:char;
begin
  try
    Sourcetable.Connection.BeginTrans;
    try
      AssignFile(F1,txtname);
      reset(f1);
      read(f1,ch);
      while not eof(f1) do
      begin
        sourceTable.Append;
        for i:=0 to fdcount do
        begin
          TmpStr:='';
          while (ch<>#9) do
          begin     //#9 is tab
            if ch<>#13 then
            begin     //#13 is enter
              if ch<>#26 then
              begin    //#26 is end
                Tmpstr:=TmpStr+ch;
                read(F1,ch);
              end else
              begin
                closefile(f1);
                exit;
              end;
            end else
            read(f1,ch);
          end;
          read(F1,ch);
          SourceTable.Fields[i].AsString:=trim(TmpStr);
        end;
      end;
    finally
      closefile(f1);
    end;
    sourcetable.Connection.CommitTrans;
  except
    sourcetable.Connection.RollbackTrans;
    messagedlg('导入数据失败!',mtinformation,[mbok],0);
  end;
end;

解决方案 »

  1.   

    //...............................
    closefile(f1);
                    exit;
                  end;
                end else
                read(f1,ch);
              end;
              read(F1,ch);
              SourceTable.Fields[i].AsString:=trim(TmpStr);
            end;
          end;
        finally //try finally ..这里的语句是必须执行的,哪怕是出现Exit也会执行..end;
          closefile(f1); //如果CloseFile()了两次,就会出现I/O错误,所以,上面的CloseFile()一个都不要
    //...............................
      

  2.   

    zswang(伴水清清)(专家门诊清洁工) 说得对