txt字典导入数据库

 

function OpenTxtToDB(flag:string;pb1: TProgressBar;lbl1: TLabel):Boolean;
var
  FileName:string;

  t: textFile;
  path, sfield: string;
  s, sql, errer: string;
  i: integer;
  jd, all: integer;
begin
  try
    try
      if dlgOpen1.Execute then
      begin
        FileName:= dlgOpen1.FileName;
        with TStringList.Create do
        begin
          try
            LoadFromFile(FileName);
            all := Count; //获取总行数
          finally
            Free;
          end;
        end;
      end
      else Exit;

      if all < 1 then
        Exit;


      AssignFile(t, FileName);
      Reset(t); //只读打开文件
      jd := 0;
      pb1.Max := all;
      pb1.Position := 1;


      while not Eof(t) do
      begin
        ReadLn(t, s);
        sfield := trim(StringReplace(s, '|', '_', [rfReplaceAll]));
        sfield := trim(StringReplace(s, ''#9'', '|', [rfReplaceAll]));
        if sfield <> '' then
        begin
          sql := sql + 'exec yb_ml_down ''' + flag + ''',' + QuotedStr(sfield) + #13#10;
          sql := StringReplace(sql, ':', '*', [rfReplaceAll]);
          if ((jd mod 20) = 0) or (jd > all - 2) then
          begin
            cds1.Close;
            cds1.data := null;
            cds1.CommandText := sql;
            try
              cds1.Execute;
            except
              on E:Exception do
            //  InsertLog('数据插入错误sql:' + sql);
              errer := errer + E.Message + #13#10 + sql;
            end;
            sql := '';
            pb1.Position := jd + 1;
            lbl1.Caption := '进度:' + inttostr(jd + 1) + '/' + inttostr(all);
            lbl1.Update;
            application.ProcessMessages;
          end;
        end;
        Application.ProcessMessages;
        jd := jd + 1;
        next;
      end;

      if Trim(errer) <> '' then
      begin
        ShowMessage('数据插入错误:' +errer);
        pb1.Position:=0;
        Exit;
      end
      else
      begin
        ShowMessage('下载完成,共更新' + inttostr(all) + '条数据');
      end;
      pb1.Position:=0;
      Result := True;

    except
      on e:Exception do
      begin

      end;
    end;
  finally
    CloseFile(t);
  end;
end;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值