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;