我有一个标准文本文件其中有多行文本,每行8个以逗号分隔的字段,怎样使用ADO组件将其中数据导入一个已经存在的Access数据表中?我在以往的帖子中搜索了一下,没有结果,特向大家请教,谢!

解决方案 »

  1.   

    你可以把文本先放到一个MEMO1中,然后将MEMO1中的数据逐行的分离成你需要的数据,然后导入,你可以通过pos得到某个子串的位置,然后通过copy获得逐个分离解析数据。给你个例子,格式是  字符(数字)数字
    type
      DataInfo = record
        sh : integer;
        qy : string;
        sl : integer;
      end;procedure Tfrm_Getdat.LoadAlldat;
    var
      rq : TDate;
    begin       rq := strtodate(copy(Memo1.Lines[1],1,10));//第一行存的是日期       dtmd.bnConnection.BeginTrans;
           //判断此日期的数据是否存在
           with dtmd.tb_insertSl do
           begin
             if not Active then Open;
             if not Locate('rq',FormatDateTime('yyyy-mm-dd',rq),[loCaseInsensitive] ) then
                InsertData(rq)
             else if MessageDlg(Format('日期【%s】的数据已经存在,要覆盖数据吗?',[DateTostr(RQ)]),mtConfirmation,[mbyes,mbno],1) = mryes then
             begin
              //删除数据
              with dtmd.query_temp do
              begin
                Close;
                SQL.Clear;
                SQL.Add('delete from slinfo where rq=:#rq');
                Parameters.ParamByName('#rq').Value := FormatDateTime('yyyy-mm-dd',RQ);
                try
                  ExecSQL;
                except
                  MessageDlg('删除数据失败!',mtError,[mbok],0);
                  ProgressBar1.Visible := false;
                  exit;
                end;
              end;
              InsertData(rq);
           end;
           dtmd.bnConnection.CommitTrans;
           end;
    end;
    function Tfrm_Getdat.Loaddat(value : string):DataInfo;//将数据转换成新的数据结构var
    s1,s2 : integer;
    begin
       //DataFormat   qy(sh)sl
        s1 := PosEx('(',value);
        s2 := PosEx(')',value);
        Result.sh := strtoint(copy(value,s1+1,s2-s1-1));
        Result.qy := copy(value,1,s1-1);
        Result.sl := StrToInt(copy(value,s2+1,length(value)-s2));
    end;procedure Tfrm_Getdat.InsertData(rq : TDate);
    var
      i : integer;
      TmpData :DataInfo;
      qy :string;
    begin
       with dtmd.tb_insertSl do
       begin
          If not Active then Open;
          ProgressBar1.Visible := true;
          ProgressBar1.Max := Memo1.Lines.Count - 3;
          for i:= 3 to 74 do
          begin
            try
              TmpData := LoadDat(Memo1.Lines[i]);
            except
              MessageDlg('数据格式错误!   ',mtError,[mbok],0);
              ProgressBar1.Visible := false;
              dtmd.bnConnection.RollbackTrans;
              exit;
            end;
            Insert;
            FieldByName('sh').Value := TmpData.sh;
            FieldByName('qy').Value := TmpData.qy;
            FieldByName('rq').Value := FormatDateTime('yyyy-mm-dd',rq);
            FieldByName('sl').Value := TmpData.sl;
            try
              Post;
              ProgressBar1.Position := ProgressBar1.Position + 1;
            except
              MessageDlg('数据添加失败!',mtError,[mbok],0);
              dtmd.bnConnection.RollbackTrans;
              ProgressBar1.Visible := false;
              Exit;
            end;
          end;
          ProgressBar1.Visible := false;
          MessageDlg('数据导入完毕!',mtInformation,[mbok],0);
       end;
    end;