数据表inventory 中的cinvcode字段是关键字,每次重复时都不会执行Application.MessageBox('已存在该款号','提示信息',MB_OKCANCEL);而是直接报错.该如何解决?procedure TForm4.Button2Click(Sender: TObject);
var
user,temp,cinv:string;begin
user:=form2.StatusBar1.Panels[3].Text;
//检测是否在数据库存在相同的记录
cinv:=trim(edit1.Text);
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Text:='select cinvcode from inventory where cinvcode='''+cinv+'''';
ADOQuery2.Open;
temp:=ADOQuery1.FieldByName('cinvcode').AsString;
if temp<>'' then
begin
Application.MessageBox('已存在该款号','提示信息',MB_OKCANCEL);
exit;
end
else
begin
try
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into inventory values('''+Edit1.Text+''','+edit2.Text+','''+user+''','''+datetostr(now)+''')');//增加
ADOQuery1.ExecSQL;
Application.MessageBox('保存成功!!!','提示信息',MB_OK);
except
Application.MessageBox('保存失败!!!','提示信息',MB_OK);
end;
end;
end;
var
user,temp,cinv:string;begin
user:=form2.StatusBar1.Panels[3].Text;
//检测是否在数据库存在相同的记录
cinv:=trim(edit1.Text);
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Text:='select cinvcode from inventory where cinvcode='''+cinv+'''';
ADOQuery2.Open;
temp:=ADOQuery1.FieldByName('cinvcode').AsString;
if temp<>'' then
begin
Application.MessageBox('已存在该款号','提示信息',MB_OKCANCEL);
exit;
end
else
begin
try
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into inventory values('''+Edit1.Text+''','+edit2.Text+','''+user+''','''+datetostr(now)+''')');//增加
ADOQuery1.ExecSQL;
Application.MessageBox('保存成功!!!','提示信息',MB_OK);
except
Application.MessageBox('保存失败!!!','提示信息',MB_OK);
end;
end;
end;
adoqry1.sql.text:='insert into .....';
try
adoqry1.execSQL;
except
end;
重复了就会插不进去
也不会报异常了。
再有判断时,直接用 if ADOQuery2.IsEmpty 判断就行了
{问题在这句,应该改为:temp:=ADOQuery2.FieldByName('cinvcode').AsString;}
temp:=ADOQuery2.FieldByName('cinvcode').AsString;
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Text:='select 1 from inventory where cinvcode='''+cinv+'''';
ADOQuery2.Open;
if not ADOQuery2.IsEmpty then
begin
Application.MessageBox('已存在该款号','提示信息',MB_OKCANCEL);
exit;
end
else begin
{...}
end;
还有就是ADOQuery2.SQL.Text:='select cinvcode from inventory where cinvcode='''+cinv+'''';
直接改成ADOQuery2.SQL.add('select * from inventory where cinvcode='+''''+cinv+'''');