procedure Tloca.Button1Click(Sender: TObject);
begin
  if Trim(Edit1.Text)='' then
    application.MessageBox('请输入要新增的库位名!','提示',mb_ok+mb_iconinformation)
  else
    begin
      dm.ADOQuery1.Close;
      dm.ADOQuery1.SQL.Clear;
      dm.ADOQuery1.SQL.Add('Select location from Exe_loca where location='''+Trim(Edit1.Text)+'''');
      try
        dm.ADOQuery1.Open;
      except
        dm.ADOQuery1.ExecSQL;
      end;
      if not dm.ADOQuery1.Eof then
        application.MessageBox('该库位已存在!','提示',mb_ok+mb_iconinformation)
      else
        begin
          dm.ADOQuery1.Close;
          dm.ADOQuery1.SQL.Clear;
          dm.ADOQuery1.SQL.Add('Insert into Exe_loca (location,putawayzone,ifmix) values('''+trim(edit1.Text)+''','''+trim(edit2.Text)+''','''+trim(combobox1.Text)+''')');
          dm.ADOQuery1.ExecSQL;
        end
      end
end;

解决方案 »

  1.   

    try
            dm.ADOQuery1.Open;
          except
            dm.ADOQuery1.ExecSQL;
    ---------------
    改为 dm.ADOQuery1.Open;
      

  2.   

    這種代碼不要用Try
    再就是你不循環,怎麼可能插入多條記錄?
      

  3.   

    并不是你插入了两条记录,而是你并没有检测到原来已经存在的记录,也就是说原来那条记录已经存在了,但是你的代码没有检测数来,然后又插入了一条,你的代码改一下:
    procedure Tloca.Button1Click(Sender: TObject);
    begin
      if Trim(Edit1.Text)='' then
        application.MessageBox('请输入要新增的库位名!','提示',mb_ok+mb_iconinformation)
      else
        begin
          dm.ADOQuery1.Close;
          dm.ADOQuery1.SQL.Clear;
          dm.ADOQuery1.SQL.Add('Select location from Exe_loca where location='+QuotedStr(Trim(Edit1.Text));
            dm.ADOQuery1.Open;
          if dm.ADOQuery1.recordcount<>0 then
            application.MessageBox('该库位已存在!','提示',mb_ok+mb_iconinformation)
          else
            begin
              dm.ADOQuery1.Close;
              dm.ADOQuery1.SQL.Clear;
              dm.ADOQuery1.SQL.Add('Insert into Exe_loca (location,putawayzone,ifmix) values('''+trim(edit1.Text)+''','''+trim(edit2.Text)+''','''+trim(combobox1.Text)+''')');
              dm.ADOQuery1.ExecSQL;
            end
          end
    end;
    如果还是不行的话,就要看看'Select location from Exe_loca where location='+QuotedStr(Trim(Edit1.Text));这句话了,是不是真正能够找出已经存在的那条记录
      

  4.   

    已经解决了,谢谢vokeyliu,顺便问下,怎么能在删除一条记录后提示用户记录已经删除了?
      

  5.   

    dm.ADOQuery1.close;
    dm.ADOQuery1.sql.clear;
    dm.ADOQuery1.sql.add('delete from 表1 where 字段<>1');
    try
       dm.ADOQuery1.ExecSQL;
       ShowMessage('记录已经删除');
    except
       on e:exception do
           ShowMessage(e.message);
    end;
    不过对于这种不返回数据集的操作我一般会使用TADOCOMMAND组件,这个要好的多。