sl:=TStringList.Create; try sl.LoadFromFile(Hanzi.txt'); adotable1.open; for i:=0 to sl.Count-1 do begin adotable1.append; adotable1.fieldbyname(aaa):=sl.strings[i]; adotable1.post; end; finally adotable1.close; sl.Free; end;
cg1120说的方法中由于你存的时候将一条记录存成一行,这样stringlist读进来后 stringlist.string[i]一个字符串是一行文本,可以按字符读,不过你要规定好两个字段之间有什么在文本中断开,下面的程序是用#9(tab)断开的; procedure txzdmfrm.TxtToDB(SourceTable:TAdotable;Txtname:TFileName); //文本入数据库 var F1:Textfile; i:integer; TmpStr:string; ch:char; begin try AssignFile(F1,txtname); reset(f1); read(f1,ch); while ch<>#26 do begin if not SourceTable.Active then SourceTable.Open; sourceTable.Append; for i:=0 to SourceTable.FieldCount-1 do begin TmpStr:=''; while (ch<>#9) do begin //#9 is tab if ch<>#13 then begin //#13 is enter if ch<>#26 then begin //#26 is line end Tmpstr:=TmpStr+ch; read(F1,ch); end else begin closefile(f1); //showmessage('ok'); exit; end; end else read(f1,ch); end; read(F1,ch); SourceTable.Fields[i].AsString:=trim(TmpStr); end;//end of (for i:=0 to SourceTable.FieldCount-1 do) end;//end of (if not eof(f1) then except closefile(f1); MessageDlg( '接收数据失败!',mtError,[mbOk],0); end; end;
try
sl.LoadFromFile(Hanzi.txt');
adotable1.open;
for i:=0 to sl.Count-1 do
begin
adotable1.append;
adotable1.fieldbyname(aaa):=sl.strings[i];
adotable1.post;
end;
finally
adotable1.close;
sl.Free;
end;
2。自己读取后,用servers控件组的TexcelApplication写道特定的文件里:)
stringlist.string[i]一个字符串是一行文本,可以按字符读,不过你要规定好两个字段之间有什么在文本中断开,下面的程序是用#9(tab)断开的;
procedure txzdmfrm.TxtToDB(SourceTable:TAdotable;Txtname:TFileName); //文本入数据库
var
F1:Textfile;
i:integer;
TmpStr:string;
ch:char;
begin
try
AssignFile(F1,txtname);
reset(f1);
read(f1,ch);
while ch<>#26 do begin
if not SourceTable.Active then SourceTable.Open;
sourceTable.Append;
for i:=0 to SourceTable.FieldCount-1 do begin
TmpStr:='';
while (ch<>#9) do
begin //#9 is tab
if ch<>#13 then
begin //#13 is enter
if ch<>#26 then
begin //#26 is line end
Tmpstr:=TmpStr+ch;
read(F1,ch);
end
else
begin
closefile(f1);
//showmessage('ok');
exit;
end;
end
else
read(f1,ch);
end;
read(F1,ch);
SourceTable.Fields[i].AsString:=trim(TmpStr);
end;//end of (for i:=0 to SourceTable.FieldCount-1 do)
end;//end of (if not eof(f1) then
except
closefile(f1);
MessageDlg( '接收数据失败!',mtError,[mbOk],0);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var sl:tstringlist;
i:integer;
begin
sl:=TStringList.Create;
sl.LoadFromFile(opendialog1.FileName);
adotab.open;
try
for i:=0 to sl.Count-1 do
begin
with adotab do
begin
append;
sl.Delimiter:=',';
sl.DelimitedText:=memo1.Lines.Text;
adotab.Fields[i].AsString:=sl.Strings[i];
post;
end;
end;
finally
adotab.close;
sl.Free;
end;
end;
比如我的记录应该是
字段1 字段2 字段3 字段4 字段5 字段6
第1条记录 1 2 3 4 5 6
当程序运行完后,存到表中的数据却是这样的,
字段1 字段2 字段3 字段4 字段5 字段6
第1条记录 1
第1条记录 2
第1条记录 3
第1条记录 4
第1条记录 5
第1条记录 6该怎么办啊?