有一文本文件(txt),里面有格式有规律的数据,需要上载到某个表中,使相应内容进入相应的字段。例如:
文本文件:
a 123 11111
b 234 22222
c 456 33333上载到table:
字段A 字段B 字段C
a 123 11111
b 234 22222
c 456 33333
该使用什么方法?
文本文件:
a 123 11111
b 234 22222
c 456 33333上载到table:
字段A 字段B 字段C
a 123 11111
b 234 22222
c 456 33333
该使用什么方法?
//这是Delphi自带的字符串处理单元,里面有很多字符串处理函数var
FileList,RecList: TStrings;
i: Integer;
begin
FileList:=TStringList.Create;
RecList:=TStringList.Create;
try
FileList.LoadFromFile('一个文本文件'); //载入文件到FileList中
for i:=0 to FileList.Count-1 do //处理FileList的每一行
begin
//将FileList的每一行拆分为用空格隔开的字符串,保存到RecList中
//分割的空格可以是一个也可以是多个,但只用写[' ']一个空格就可以了
ExtractStrings([' '],[],FileList.Strings[i],RecList);
Table1.append; //添加记录
Table1.FieldByName('字段1').AsString:=RecList.Strings[0];
Table1.FieldByName('字段2').AsString:=RecList.Strings[1];
Table1.FieldByName('字段3').AsString:=RecList.Strings[2];
Table1.post;
RecList.Clear;
end;
finally
FileList.Free; //清除内存
RecList.Free;
end;
end;
ExtractStrings([' '],[],PChar(FileList.Strings[i]),RecList);
EXEC master..xp_cmdshell 'bcp dbname..tablename in c:\DT.txt -c -Sservername -Usa -Ppassword'