procedure TForm1.btnimporttextClick(Sender: TObject); var TextFileVar: Textfile ; fileN,s: string; testcode,name,school,grade,temp:string; num:integer; begin num:=0; if opendialog1.execute then begin FileN:=opendialog1.FileName; If FileN='|' Then Exit; AssignFile ( TextFileVar , FileN ) ; Reset(TextFileVar); memo1.Lines.LoadFromFile(opendialog1.FileName); if messagedlg('确定要将该文本导入数据库吗?',mtconfirmation,[mbyes,mbno],0)=mryes then begin while not SeekEof(TextFileVar) do begin Readln ( TextFileVar , temp ) ; if pos('|',temp)>0 then begin testcode:=copy(temp, 1, pos('|',temp)-1); temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp)); name:=copy(temp, 1, pos('|',temp)-1); temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp)); school:=copy(temp, 1, pos('|',temp)-1); temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp)); grade:=copy(temp, 1, pos('|',temp)-1); adoquery1.Close; adoquery1.SQL.Clear; adoquery1.SQL.Add('select testcode from pystudent where testcode='+testcode); adoquery1.Open; if adoquery1.RecordCount=0 then begin adotable1.Open; adotable1.insert; adotable1.fields[0].AsString:=testcode; adotable1.fields[1].AsString:=name; adotable1.fields[2].AsString:=school; adotable1.Fields[3].AsString:=grade; adotable1.post; end else num:=num+1; adoquery1.Close; s:=inttostr(num); end; end; showmessage('有'+s+'条记录相同而未导入数据库'); closeFile(TextFileVar); adotable1.Close; showmessage('有效文件成功导入数据库'); end; end;
1 你的数组下标没有增加 2 用tstrings的例子 s:string; ss:tstrings; begin ss:=tstrins.create(); ... s:=readln(f); while s<>'' do begin ss.add(s); s:=readln(f); end;
var
TextFileVar: Textfile ;
fileN,s: string;
testcode,name,school,grade,temp:string;
num:integer;
begin
num:=0;
if opendialog1.execute then
begin
FileN:=opendialog1.FileName;
If FileN='|' Then
Exit;
AssignFile ( TextFileVar , FileN ) ;
Reset(TextFileVar);
memo1.Lines.LoadFromFile(opendialog1.FileName);
if messagedlg('确定要将该文本导入数据库吗?',mtconfirmation,[mbyes,mbno],0)=mryes then
begin
while not SeekEof(TextFileVar) do
begin
Readln ( TextFileVar , temp ) ;
if pos('|',temp)>0 then
begin
testcode:=copy(temp, 1, pos('|',temp)-1);
temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp));
name:=copy(temp, 1, pos('|',temp)-1);
temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp));
school:=copy(temp, 1, pos('|',temp)-1);
temp:=copy(temp,pos('|',temp)+1,length(temp)-pos('|',temp));
grade:=copy(temp, 1, pos('|',temp)-1);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select testcode from pystudent where testcode='+testcode);
adoquery1.Open;
if adoquery1.RecordCount=0 then
begin
adotable1.Open;
adotable1.insert;
adotable1.fields[0].AsString:=testcode;
adotable1.fields[1].AsString:=name;
adotable1.fields[2].AsString:=school;
adotable1.Fields[3].AsString:=grade;
adotable1.post;
end
else
num:=num+1;
adoquery1.Close;
s:=inttostr(num);
end;
end;
showmessage('有'+s+'条记录相同而未导入数据库');
closeFile(TextFileVar);
adotable1.Close;
showmessage('有效文件成功导入数据库');
end;
end;
2 用tstrings的例子
s:string;
ss:tstrings;
begin
ss:=tstrins.create();
...
s:=readln(f);
while s<>'' do
begin
ss.add(s);
s:=readln(f);
end;