delphi txt字典导入数据库  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 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;

 

————————————————


原文链接:https://blog.csdn.net/missingzlp/article/details/90666514



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.035408973693848 seconds