数据更新出了问题,我在保存学生信息的同时需要将学号、姓名保存到一张临时表中。但是在更新的时候提示由于将索引主关键字创建重复值的提示,保存不成功
请大虾给出修改时候同步更新临时表中数据的代码,多谢
学生表
stuid 学号
name 姓名
birth 生日
per_id 考勤卡号TMP表
per_name 姓名
per_card 生日
per_id 考勤卡号
考勤卡号是唯一的
请大虾给出修改时候同步更新临时表中数据的代码,多谢
学生表
stuid 学号
name 姓名
birth 生日
per_id 考勤卡号TMP表
per_name 姓名
per_card 生日
per_id 考勤卡号
考勤卡号是唯一的
但是修改了某个信息如学号或姓名再保存的时候就出错了,修改的某个学生的信息不能同步更新到TMP表中,有谁能给出相关的代码,谢谢
修改后再取出新的学号和新的姓名strNewID,strNewName
然后,if strOldID <> strNewID then
begin
strSQL := 'Update tmp set per_id= ' + strNewID +
' Where per_id='+ strOldID;
//执行strSQL
end;if strOldName <> strNewName then
begin
strSQL := 'Update tmp set per_name=' + QuotedStr(strNewName) +
' Where per_name = ' + QuotedStr(strOldName);
//执行strSQL
end;
第二个的if strOldName <> strNewName then
好像是
if strOldName = strNewName then
修改保存后,怎么知道修改后的记录就是原来的的记录呢?
判断新旧字段的操作,应该在哪个事件中实现呢?
请告之,谢谢
procedure TForm1.AQBeforePost(DataSet: TDataSet);
begin
if aq.State=dsinsert then
begin
aqtmp.Insert;
aqtmp.FieldByName('stuid').AsString:=dbediteh2.Text;
aqtmp.FieldByName('per_name').AsString:=dbediteh1.Text;
aqtmp.FieldByName('per_card').AsString:=dbediteh3.Text;
aqtmp.FieldByName('per_id').AsString:='01';
aqtmp.FieldByName('per_yes').AsString:='N';
aqtmp.Post;
end;
if aq.State=dsedit then
begin
aqtmp.FieldByName('stuid').AsString:=dbediteh2.Text;
aqtmp.FieldByName('per_name').AsString:=dbediteh1.Text;
aqtmp.FieldByName('per_card').AsString:=dbediteh3.Text;
aqtmp.FieldByName('per_id').AsString:='01';
aqtmp.FieldByName('per_yes').AsString:='N';
aqtmp.Post;
end;
end;
btnEdit.OnClick中strOldID := FieldByName('per_id').AsString;
strOldName := FieldByName('per_name').AsString;//开始修改
Edit;
FieldByName('per_id').AsString := edtId.Text;
FieldByName(per_name'').AsString := edtName.Text;
Post;strNewID := FieldByName('per_id').AsString;
strNewName := FieldByName('per_name').AsString;然后是我刚才写的代码....
我将主要的代码贴出来,各位看看,看看错误的地方
其中AQ是student表的ADOQUERY,AQTMP是TMP表的ADOQUERY,我将更新的操作在AQ的beforepost事件中实现,代码如下:stuid 是学生学号,per_name是学生姓名,per_card考勤卡号procedure TForm1.BitBtn2Click(Sender: TObject);//该段就是执行保存操作
begin
if aq.State in [dsinsert,dsedit] then
begin
try
aq.Post;
except
end;
end;
end;procedure TForm1.AQBeforePost(DataSet: TDataSet);//执行更新操作
var
query: tadoquery;
id:integer;
strOldID,strOldName,strNewID,strNewName,stroldcard,strnewcard,strsql :string;
begin
if aq.State = dsinsert then
begin
try
aqtmp.Insert;
aqtmp.FieldByName('stuid').AsString := dbediteh2.Text;
aqtmp.FieldByName('per_name').AsString := dbediteh1.Text;
aqtmp.FieldByName('per_card').AsString := dbediteh3.Text;//考勤卡号赋值
aqtmp.Post;
except
end;
end;
if aq.State=dsedit then
begin
with aqtmp do
begin
strOldID := trim(FieldByName('stuid').AsString);
strOldName := FieldByName('per_name').AsString;
stroldcard:=FieldByName('per_card').asstring; FieldByName('stuid').AsString := dbediteh2.Text;
FieldByName('name').AsString :=dbediteh1.Text;
FieldByName('per_card').AsString :=dbediteh3.Text;
Post; end;
strNewID := trim(aqtmp.FieldByName('stuid').AsString);
strNewName := aq.FieldByName('name').AsString;
strnewcard:= aq.fieldbyname('per_card').asstring; if strOldID <> strNewID then
begin
with aqtmp do
begin
strSQL := 'Update tmp set stuid= ' + strNewID +
' Where stuid='+ strOldID;
close;
sql.Clear;
sql.Text:=strsql;
execsql;
end;
end;if strOldName <> strNewName then
begin
with aqtmp do
begin
strSQL := 'Update tmp set per_name=' + QuotedStr(strNewName) +
' Where per_name = ' + QuotedStr(strOldName);
close;
sql.Clear;
sql.Text:=strsql;
execsql;
end;
end;
end;
end;
请问各位怎么才能使我在修改学生记录的时候,更新到tmp中时候怎么才能让更新的记录就是
修改的记录啊
update tmp set stuid='111',per_card='222',per_name='333'where stuid='xxx';