实现代码:
procedure TForm3.N17Click(Sender: TObject);
var
AList:TStringList;
AAdq:TADOQuery;
AXuYaoArray:TArray<string>;
I:Integer;
OpenDialog:TOpenDialog;
begin
AList:=TStringList.Create;
AAdq:=TADOQuery.Create(nil);
AAdq.Connection:=frmDataPool.ADOConnection1;
OpenDialog:=TOpenDialog.Create(nil);
OpenDialog.DefaultExt:='txt';
OpenDialog.Filter:='Text files (*.txt)|*.TXT';
try
//从记事本获得数据
try
if OpenDialog.Execute then
begin
AList.LoadFromFile(OpenDialog.FileName);
end;
except
on EReadError do
ShowMessage('打开文件失败');
end;
//取值读取赋值给数组
for I := 0 to AList.Count-1 do
begin
//stream.Position := 0 ;
if (AList[I]<>'') and (AList[I]<>'选中|ID|类别|标题|优先级|状态|已完成百分比|说明|开始日期|截止日期|附件|') then
begin
AXuYaoArray:=AList[I].Split([';']);
//写入数据库
with AAdq do
begin
Close;
SQL.Text:='INSERT INTO 需要做的事(选中,类别,标题,优先级,状态,已完成百分比,说明,开始日期,截止日期,附件)VALUES(:选中,:类别,:标题,:优先级,:状态,:已完成百分比,:说明,:开始日期,:截止日期,:附件) ';
Parameters.ParamByName('选中').Value:=AXuYaoArray[0].ToBoolean;
//Parameters.ParamByName('ID').Value:=AXuYaoArray[1].ToInteger;
Parameters.ParamByName('类别').Value:=AXuYaoArray[2];
Parameters.ParamByName('标题').Value:= AXuYaoArray[3];
Parameters.ParamByName('优先级').Value:= AXuYaoArray[4];
Parameters.ParamByName('状态').Value:= AXuYaoArray[5];
Parameters.ParamByName('已完成百分比').Value:= AXuYaoArray[6].ToExtended;
Parameters.ParamByName('说明').Value:= AXuYaoArray[7];
Parameters.ParamByName('开始日期').Value:= FormatDateTime('yyyy-mm-dd',StrToDateTime(AXuYaoArray[8]));
Parameters.ParamByName('截止日期').Value:= FormatDateTime('yyyy-mm-dd',StrToDateTime(AXuYaoArray[8])+7);
Parameters.ParamByName('附件').Value:= AXuYaoArray[10];
ExecSQL;
end;
end;
Application.ProcessMessages;
frmDataPool.qry需要做的事.Requery();
end;
finally
AList.Free;
AAdq.Free;
OpenDialog.Free;
end;
end;