2,507
社区成员




procedure TForm2.Button1Click(Sender: TObject);
var
Strs:TStringList;
strs2:TStringList;
i,n:Integer;
codeage,sexno: integer ;
Sql:string;
begin
codeage:=1960;
sexno:=1;
Strs:=TStringList.Create;
strs2:=TStringList.Create;
Strs.LoadFromFile(AdvFileNameEdit1.Text);
SetLength(ErrStr,Strs.Count);
AdvColumnGrid1.RowCount:=Strs.Count;
DB.BeginTransaction;
for I := 0 to Strs.Count - 1 do
begin
Application.ProcessMessages;
strs2.Delimiter:='|';
strs2.DelimitedText:=Strs[i];
if (Strs2.Count<6) or( Strs2.Count>6)then
begin
ErrStr[i]:='第'+inttostr(i+1)+'条记录导入格式错误!';
continue;
end;
if DataModule4.IsChar(strs2[0])=False then
begin
ErrStr[i]:='第'+inttostr(i+1)+'条记录第一部分文本错误!' ;
continue;
end;
if DataModule4.IsChar(strs2[1])=False then
begin
ErrStr[i]:='第'+inttostr(i+1)+'条记录第二部分文本错误!' ;
continue;
end;
if DataModule4.IsChar(strs2[2])=False then
begin
ErrStr[i]:='第'+inttostr(i+1)+'条记录第三部分文本错误!' ;
continue;
end;
if DataModule4.Ident(strs2[3],codeage,sexno)<>''then
begin
ErrStr[i]:='第'+inttostr(i+1)+'条记录身份证号码错误!';
continue;
end;
AdvColumnGrid1.Cells[1,i+1]:=strs2[0];
AdvColumnGrid1.Cells[2,i+1]:=strs2[1];
AdvColumnGrid1.Cells[3,i+1]:=strs2[2];
AdvColumnGrid1.Cells[4,i+1]:=strs2[3];
AdvColumnGrid1.Cells[5,i+1]:=strs2[4];
AdvColumnGrid1.Cells[6,i+1]:=strs2[5];
try
Sql:='select '+QuotedStr(strs2[0])+' from town where town='+QuotedStr(strs2[0]);
if DB.GetTable(Sql).EOF then
begin
Sql:='insert into town(T_id,town)values((select max(T_id) from town)+1,'+QuotedStr(strs2[0])+')' ;
DB.ExecSQL(sql);
end;
Sql:='select * from Village where VillageN='+QuotedStr(strs2[1]);
if DB.GetTable(Sql).EOF then
begin
Sql:='insert into Village(V_id,T_id,VillageN)values((select max(V_id) from Village)+1 ,(select T_id from town where town='+QuotedStr(strs2[0])+'),'+QuotedStr(strs2[1])+')' ;
DB.ExecSQL(sql);
end;
Sql:='select * from people where idno='+QuotedStr(strs2[3]);
if DB.GetTable(sql).EOF then
begin
sql:='insert into people(T_id,V_id,name,idno,plans,Copies)values((select T_id from town where town='+QuotedStr(strs2[0])+'),(select V_id from Village where Villagen='+QuotedStr(strs2[1])+'),'+QuotedStr(strs2[2])+','+QuotedStr(strs2[3])+','+QuotedStr(strs2[4])+','+QuotedStr(strs2[5])+')';
DB.ExecSQL(sql);
end else
begin
sql:='update people set times=(select times from people where idno='+QuotedStr(strs2[3])+')+1 where idno='+QuotedStr(strs2[3]) ;
DB.ExecSQL(sql);
end;
except
ShowMessage('导入失败');
end;
Application.ProcessMessages;
end;
for n:=Low (ErrStr) to High(ErrStr) do
begin
if errstr[n]<>''then
ShowMessage(errstr[n]);
end;
Strs.Free;
strs2.Free;
SetLength(ErrStr,0);
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if DB.IsTransactionOpen then
begin
case Application.MessageBox('是否保存导入数据', '提示', MB_OKCANCEL + MB_ICONQUESTION
+ MB_TOPMOST) of
IDOK:
begin
DB.Commit;
end;
IDCANCEL:
begin
DB.Rollback;
end;
end;
end;
if DB.IsTransactionOpen=False then
begin
DB.Free;
Action := caFree;
form2 := nil;
end;