我有一个16进制文件,根据定长截取记录(有可能会有10万条),再把记录根据位置截取对应的值保存,不知道如何处理速度会快点。我现在是通过FileStream读入文件,然后根据定长截取,感觉速度比较慢!
不知道大家有什么好的方法。
var
ValueBytes : Array of byte;
fs : TFileStream ;
begin
......................................
fs := TFileStream.Create('.\jywj\' + jyfilename,fmOpenRead);
SetLength(ValueBytes,fs.Size);
fs.Read(ValueBytes[0],fs.Size);
for k:=0 to ((length(ValueBytes) -28) div 374 - 1) do //(length(ValueBytes) -28) div 374
begin
Form_rzcl.ADOQuery.Close;
Form_rzcl.ADOQuery.SQL.Clear;
Form_rzcl.ADOQuery.SQL.Add('insert into log_zw(cx,jlh,czrq,wjjmc,rzwj,jywj,Type,CarNo,CodeID,Count,DateTimeOccUTC,DateTimeGoneUTC,EventCount,EventStatus)');
Form_rzcl.ADOQuery.SQL.Add(' values (:cx,:jlh,:czrq,:wjjmc,:rzwj,:jywj,:Type,:CarNo,:CodeID,:Count,:DateTimeOccUTC,:DateTimeGoneUTC,:EventCount,:EventStatus) ') ;
Form_rzcl.ADOQuery.Parameters.ParamByName('cx').Value := cx ;
Form_rzcl.ADOQuery.Parameters.ParamByName('jlh').Value := k+1 ;
Form_rzcl.ADOQuery.Parameters.ParamByName('czrq').Value := FormatDateTime('YYYY-MM-DD HH:mm:ss',Now()) ;
Form_rzcl.ADOQuery.Parameters.ParamByName('wjjmc').Value := wjjmc ;
Form_rzcl.ADOQuery.Parameters.ParamByName('rzwj').Value := filename ;
Form_rzcl.ADOQuery.Parameters.ParamByName('jywj').Value := jyfilename ;
Form_rzcl.ADOQuery.Parameters.ParamByName('type').Value := PSmallInt(@ValueBytes[28+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('CarNo').Value := PInteger(@ValueBytes[34+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('CodeID').Value := PInteger(@ValueBytes[50+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('Count').Value := PSmallInt(@ValueBytes[56+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('DateTimeOccUTC').Value := UTimeToDateTime(PInteger(@ValueBytes[60+k*374])^);
Form_rzcl.ADOQuery.Parameters.ParamByName('DateTimeGoneUTC').Value := UTimeToDateTime(PInteger(@ValueBytes[68+k*374])^);
Form_rzcl.ADOQuery.Parameters.ParamByName('EventCount').Value := PInteger(@ValueBytes[76+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('EventStatus').Value := PSmallInt(@ValueBytes[80+k*374])^;
Form_rzcl.ADOQuery.Prepared;
Form_rzcl.ADOQuery.ExecSQL;
end; ...........................................................
不知道大家有什么好的方法。
var
ValueBytes : Array of byte;
fs : TFileStream ;
begin
......................................
fs := TFileStream.Create('.\jywj\' + jyfilename,fmOpenRead);
SetLength(ValueBytes,fs.Size);
fs.Read(ValueBytes[0],fs.Size);
for k:=0 to ((length(ValueBytes) -28) div 374 - 1) do //(length(ValueBytes) -28) div 374
begin
Form_rzcl.ADOQuery.Close;
Form_rzcl.ADOQuery.SQL.Clear;
Form_rzcl.ADOQuery.SQL.Add('insert into log_zw(cx,jlh,czrq,wjjmc,rzwj,jywj,Type,CarNo,CodeID,Count,DateTimeOccUTC,DateTimeGoneUTC,EventCount,EventStatus)');
Form_rzcl.ADOQuery.SQL.Add(' values (:cx,:jlh,:czrq,:wjjmc,:rzwj,:jywj,:Type,:CarNo,:CodeID,:Count,:DateTimeOccUTC,:DateTimeGoneUTC,:EventCount,:EventStatus) ') ;
Form_rzcl.ADOQuery.Parameters.ParamByName('cx').Value := cx ;
Form_rzcl.ADOQuery.Parameters.ParamByName('jlh').Value := k+1 ;
Form_rzcl.ADOQuery.Parameters.ParamByName('czrq').Value := FormatDateTime('YYYY-MM-DD HH:mm:ss',Now()) ;
Form_rzcl.ADOQuery.Parameters.ParamByName('wjjmc').Value := wjjmc ;
Form_rzcl.ADOQuery.Parameters.ParamByName('rzwj').Value := filename ;
Form_rzcl.ADOQuery.Parameters.ParamByName('jywj').Value := jyfilename ;
Form_rzcl.ADOQuery.Parameters.ParamByName('type').Value := PSmallInt(@ValueBytes[28+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('CarNo').Value := PInteger(@ValueBytes[34+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('CodeID').Value := PInteger(@ValueBytes[50+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('Count').Value := PSmallInt(@ValueBytes[56+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('DateTimeOccUTC').Value := UTimeToDateTime(PInteger(@ValueBytes[60+k*374])^);
Form_rzcl.ADOQuery.Parameters.ParamByName('DateTimeGoneUTC').Value := UTimeToDateTime(PInteger(@ValueBytes[68+k*374])^);
Form_rzcl.ADOQuery.Parameters.ParamByName('EventCount').Value := PInteger(@ValueBytes[76+k*374])^;
Form_rzcl.ADOQuery.Parameters.ParamByName('EventStatus').Value := PSmallInt(@ValueBytes[80+k*374])^;
Form_rzcl.ADOQuery.Prepared;
Form_rzcl.ADOQuery.ExecSQL;
end; ...........................................................
把文件整理成一条一条的SQL插入语句,用分号(;)隔开,
然后用AdoQuery一次加入多行,比如1000行,提交执行,要快很多