var f:textfile; a:array of char; i:integer; begin AssignFile(F, 'a.txt'); Reset(F); i:=0; while not eof(f) do begin Readln(F,a[i]); inc(i); end; CloseFile(F); end;
我的項目中就是這樣; procedure Tgetdataform.BtngetdataClick(Sender: TObject); var dataname,linedata,timeinfo,staidinfo,dayinfo,sqlcount,sqlinst,sqlupdt:string; data: array of array of string; status,datetime1,datetime2,datetime3,sqlonduty:string; i,n:integer; datafile:TextFile; delta:boolean; begin inherited; delta:=True; i:=0; If Opendialog1.Execute then begin dataname:=Opendialog1.FileName ; End; AssignFile(datafile,dataname); If FileExists(dataname) then begin Reset(datafile); while Not Eof(datafile) do begin Readln(datafile,linedata); i:=i+1; SetLength(data,i,4); timeinfo:=copy(linedata,4,4); timeinfo:= timeinfo+'/'+copy(linedata,8,2); timeinfo:= timeinfo+'/'+copy(linedata,10,2); timeinfo:= timeinfo+' '+copy(linedata,12,2); timeinfo:= timeinfo+':'+copy(linedata,14,2); status:=copy(linedata,17,1); dayinfo:= copy(linedata,4,4); dayinfo:= dayinfo+'/'+copy(linedata,8,2); dayinfo:= dayinfo+'/'+copy(linedata,10,2); staidinfo:=copy(linedata,19,length(linedata)-18); data[i-1,0]:=timeinfo; data[i-1,1]:=staidinfo; data[i-1,2]:=dayinfo; data[i-1,3]:=status; end; CloseFile(datafile); End; dm.Database1.StartTransaction ; try //dm.query1.Database. For n:=0 to i-1 do begin sqlcount:='select count(*) as c from tab_staff where sta_id= '''+data[n,1]+''' '; try dm.query2.close; dm.query2.SQL.clear; dm.query2.SQL.add(sqlcount); dm.query2.open ; Except showmessage('數據庫錯誤!!'); end; if dm.query2.FieldByName ('c').asinteger =0 then delta:=False; If delta then begin sqlcount:='select count(*) as c from tab_chkduty where sta_id= '''+data[n,1]+''' and chk_day between '''+data[n,2]+''' and '''+data[n,2]+''' '; try strtodatetime(data[n,2]); except showmessage('aa日期輸入無效'); end; if data[n,2]<>formatdatetime('yyyy"/"mm"/"dd',strtodatetime(data[n,2])) then begin showmessage('bb考勤日期輸入無效!'); end; try dm.query2.close; dm.query2.SQL.clear; dm.query2.SQL.add(sqlcount); dm.query2.open ; Except showmessage('數據庫錯誤!!'); end; if dm.query2.FieldByName ('c').asinteger =0 then case StrToInt(data[n,3]) of 1:begin //showmessage('這天這個工號的考勤記錄不存在,插入操作!'); datetime1:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,0])); datetime2:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,2]+' 07:40')); datetime3:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,2]+' 07:46')); if (StrToDateTime(datetime1)>StrToDateTime(datetime2)) and (StrToDateTime(datetime1)<StrToDateTime(datetime3)) then data[n,0]:=datetime2; sqlinst:='insert into tab_chkduty(sta_id,chk_day,chk_onduty,modtime) '; sqlinst:=sqlinst+'values('''+data[n,1]+''','''+data[n,2]+''','''+data[n,0]+''','''+FormatDatetime('yyyymmdd hh:nn:ss',Date)+''' )'; try dm.query1.close; dm.query1.SQL.clear; dm.query1.SQL.add(sqlinst); dm.query1.execsql; //showmessage('您已經成功添加了一條員工的考勤信息!'); Except showmessage('2數據庫錯誤!!'); end; end; dm.Database1.commit; showmessage('數據導入數據庫全部成功!!'); except dm.Database1.Rollback; showmessage('數據插入數據庫全部失敗!!'); end;end;
f:textfile;
a:array of char;
i:integer;
begin
AssignFile(F, 'a.txt');
Reset(F);
i:=0;
while not eof(f) do
begin
Readln(F,a[i]);
inc(i);
end;
CloseFile(F);
end;
procedure Tgetdataform.BtngetdataClick(Sender: TObject);
var
dataname,linedata,timeinfo,staidinfo,dayinfo,sqlcount,sqlinst,sqlupdt:string;
data: array of array of string;
status,datetime1,datetime2,datetime3,sqlonduty:string;
i,n:integer;
datafile:TextFile;
delta:boolean;
begin
inherited;
delta:=True;
i:=0;
If Opendialog1.Execute then
begin
dataname:=Opendialog1.FileName ;
End;
AssignFile(datafile,dataname);
If FileExists(dataname) then
begin
Reset(datafile);
while Not Eof(datafile) do
begin
Readln(datafile,linedata);
i:=i+1;
SetLength(data,i,4);
timeinfo:=copy(linedata,4,4);
timeinfo:= timeinfo+'/'+copy(linedata,8,2);
timeinfo:= timeinfo+'/'+copy(linedata,10,2);
timeinfo:= timeinfo+' '+copy(linedata,12,2);
timeinfo:= timeinfo+':'+copy(linedata,14,2);
status:=copy(linedata,17,1); dayinfo:= copy(linedata,4,4);
dayinfo:= dayinfo+'/'+copy(linedata,8,2);
dayinfo:= dayinfo+'/'+copy(linedata,10,2); staidinfo:=copy(linedata,19,length(linedata)-18);
data[i-1,0]:=timeinfo;
data[i-1,1]:=staidinfo;
data[i-1,2]:=dayinfo;
data[i-1,3]:=status; end;
CloseFile(datafile);
End;
dm.Database1.StartTransaction ;
try
//dm.query1.Database.
For n:=0 to i-1 do
begin
sqlcount:='select count(*) as c from tab_staff where sta_id= '''+data[n,1]+''' ';
try
dm.query2.close;
dm.query2.SQL.clear;
dm.query2.SQL.add(sqlcount);
dm.query2.open ;
Except
showmessage('數據庫錯誤!!');
end;
if dm.query2.FieldByName ('c').asinteger =0 then
delta:=False;
If delta then
begin
sqlcount:='select count(*) as c from tab_chkduty where sta_id= '''+data[n,1]+''' and chk_day between '''+data[n,2]+''' and '''+data[n,2]+''' ';
try
strtodatetime(data[n,2]);
except
showmessage('aa日期輸入無效');
end;
if data[n,2]<>formatdatetime('yyyy"/"mm"/"dd',strtodatetime(data[n,2])) then
begin
showmessage('bb考勤日期輸入無效!');
end; try
dm.query2.close;
dm.query2.SQL.clear;
dm.query2.SQL.add(sqlcount);
dm.query2.open ;
Except
showmessage('數據庫錯誤!!');
end;
if dm.query2.FieldByName ('c').asinteger =0 then
case StrToInt(data[n,3]) of
1:begin
//showmessage('這天這個工號的考勤記錄不存在,插入操作!');
datetime1:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,0]));
datetime2:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,2]+' 07:40'));
datetime3:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,2]+' 07:46'));
if (StrToDateTime(datetime1)>StrToDateTime(datetime2)) and (StrToDateTime(datetime1)<StrToDateTime(datetime3)) then
data[n,0]:=datetime2;
sqlinst:='insert into tab_chkduty(sta_id,chk_day,chk_onduty,modtime) ';
sqlinst:=sqlinst+'values('''+data[n,1]+''','''+data[n,2]+''','''+data[n,0]+''','''+FormatDatetime('yyyymmdd hh:nn:ss',Date)+''' )';
try
dm.query1.close;
dm.query1.SQL.clear;
dm.query1.SQL.add(sqlinst);
dm.query1.execsql;
//showmessage('您已經成功添加了一條員工的考勤信息!');
Except
showmessage('2數據庫錯誤!!');
end;
end;
dm.Database1.commit;
showmessage('數據導入數據庫全部成功!!');
except
dm.Database1.Rollback;
showmessage('數據插入數據庫全部失敗!!');
end;end;
我觉得你可以使用TStrings类,这个类有LoadFromFile和SaveToFile方法
用这个方法从文件中读出数据后,再把TString.Items赋给数组即可。