4楼: |
procedure Tdm.ImportCSV(const asql: string);
var MyTextFile:Textfile; str, AFileName:string; i,j,k,H,jl:integer; dd :array of string; TimeCount: TStopwatch; arr: TArray<String>;
begin
OpenDialog1.Title := '请选择要打开的文件'; OpenDialog1.Filter := '数据文件(*.txt)|*.txt|*.CSV|*.*';
if OpenDialog1.Execute() then
begin
// showmessage( OpenDialog1.FileName);
Screen.Cursor:=crHourGlass; Application.CreateForm(Twaitform,waitform); waitform.Panel1.Caption :='正在导入数据,请等候..........'; waitform.Show; waitform.Update ;
AssignFile(MyTextFile,OpenDialog1.FileName ); TimeCount := TStopwatch.Create; TimeCount.Start;
Reset(MyTextFile);
// ASQL:='INSERT INTO TEST(ID,NAME) VALUES (:ID,:NAME)';
FQRY.FetchOptions.AutoClose := False; // FQRY.SQL.Text := ASQL; jl:=100000; FQRY.Params.ArraySize:=jl ; //准备把上面的语句执行 j 次
H:=0; J:=0;
// 文件第一行是标题
if True then
Readln(MyTextFile, str);
while not Eof(MyTextFile) do begin Readln(MyTextFile, str);
str:=str.Replace('"', ''); arr:=str.Split([',']); for K := 0 to fqry.ParamCount -1 do BEGIN FQRY.Params[K].AsStrings[H] :=arr[k];
END;
inc(h); if (H=jl) OR Eof(MyTextFile) then BEGIN J:=J+H;
FConn.StartTransaction; TRY FQRY.Execute(h-1, 0); FConn.Commit; //提交 h:=0; except
on E: Exception do begin dm.Fconn.Rollback ; // showmessage(asql); // Application.ShowException(e.className,':',e.Message); end;
END ;
end;
END;
end;
CloseFile(MyTextFile);
TimeCount.Stop;
Screen.Cursor:=crDefault; waitform.FREE; // showmessage(inttostr( Length(arr))); showmessage(format('追加记录 %D 条, 用时:%f秒' ,[J, TimeCount.ElapsedMilliseconds / 1000]));
end;
----------------------------------------------
-
|