aflag :=false;
record_no :=0;
j :=0;
if (MessageDlg('确定您的输入?',mtConfirmation,[mbyes,mbNo],0)=mrYes)
then begin
//开始录入学生成绩
//record_no :=0;
With Data_Mod.DataModule1.score_Tab do
begin
close ;
TableName :='score';
active :=true;
for i:=1 to Form5.row_no do
begin
if (Form5.StringGrid1.Cells[2,i]='' )
then begin
showmessage('学生编号为空,请核查');
continue;
end ;
//showmessage(Form5.StringGrid1.Cells[2,i]);
if aflag =false
then begin
a_flag :=Locate('KCID',Form4.kcid ,[loCaseInsensitive]);
if (a_flag =false)
then begin
showmessage('在成绩表中没有该课程记录,不能够录入,请核查');
//exit;
continue ;
end
else begin
// record_No:=0;
form5.StringGrid1.Row:=1;
{if (Form5.StringGrid1.Cells[4,i]<>'' )
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+Form4.kc_mingcheng+'已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end; }
a_flag :=Locate('KcId;Id',Vararrayof([Form4.kcid,Form5.StringGrid1.Cells[2,i]]),[loCaseInsensitive]);
if a_flag =true
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+' 课程'+Form4.kc_mingcheng+' 已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end
else Inc(record_No);
Edit;
FieldByName('Id').value :=Form5.StringGrid1.Cells[2,i];
FieldByName('score').value :=Form5.StringGrid1.Cells[4,i];
Post;
refresh;
aflag :=true;
end;
end
else begin
{if (Form5.StringGrid1.Cells[4,i]<>'' )
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+Form4.kc_mingcheng+'已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end;}
a_flag :=Locate('KcId;Id',Vararrayof([Form4.kcid,Form5.StringGrid1.Cells[2,i]]),[loCaseInsensitive]);
if a_flag =true
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+' 课程'+Form4.kc_mingcheng+' 已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end
else Inc(record_No);
Append;
FieldByname('KcId').Value :=Form4.kcid ;
FieldByName('Id').value :=Form5.StringGrid1.Cells[2,i];
FieldByName('score').value :=Form5.StringGrid1.Cells[4,i];
Post;
refresh;
end;
end;
end; //end with
showmessage('录入学生成绩'+inttostr(record_no)+'成功'+#13+#10+'覆盖学生成绩'+inttostr(j)+'成功');
end
else begin
exit;
end;
end;
如果是大型数据库(比如sqlserver,因为我对它比较熟),支持多语句和流程控制,可以这样写(伪码)
adoquery.sql.clear;
for each row do
begin
adoquery.sql.add("if exists(select * from table where f1= col1 and f2=col2 and ... and fn=coln) then begin");
adoquery.sql.add("update table set f1=col1, ..., fn=coln where f1=col1 and f2=col2 and ... and fn=coln end");
adoquery.sql.add("else being insert table values(col1, col2, ..., coln) end");
end;
adoquery.execsql;