为了防止重复添加,想到读数据库中记录的count,如果count>0就表示出现了重复,否则就可以添加,写的代码出问题了,求教!
     aq2:=tadoquery.Create(self);
     aq2.Connection:=fdbtest.ADOConnection1;
     aq2.Close;
     aq2.SQL.Clear;
     aq2.SQL.Add('select count(*) as cnt from pxda ');
     aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
     aq2.ExecSQL;
     if(aq2.Parameters.ParamByName('cnt').Value=0) then
     try
     aq1.ExecSQL;
     except
     end;
其中:a,:b这些参数前面已经附值了的,我按F9run的时候没问题,插入数据的时候aq2.Parameters.ParamByName('cnt').Value=0提示没有cnt这个东东,求高手进来帮我解决问题,谢谢!!!

解决方案 »

  1.   

      aq2:=tadoquery.Create(self);
      aq2.Connection:=fdbtest.ADOConnection1;
      aq2.Close;
      aq2.SQL.Clear;
      aq2.SQL.Add('select count(*) as cnt from pxda ');
      aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
      aq2.open;
      if aq2.recordcount > 0 then
      try
      aq1.ExecSQL;
      except
      end;
      

  2.   

    查询要用 open修改和插入 用ExecSQL
      

  3.   

    回复小V  那不应该是recordcount>0吧  我的a1.execsql就是执行插入  但是我改成recordcount=0发现插入不了数据了
      

  4.   

    showmessage(aq2.sql.text); 看看你的语句是否有问题
      

  5.   

    发现aq2的sql原句返回了,估计是那几个参数没传到值,那应该怎样动态传值给这几个参数呢?
      

  6.   

    aq2.SQL.Add('select count(*) as cnt from pxda ');
    aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
    aq2.Parameters.ParamByName('a').Value:= ..;
    aq2.Parameters.ParamByName('b').Value:= ..;
    aq2.Parameters.ParamByName('c').Value:= ..;
    aq2.Parameters.ParamByName('d').Value:= ..;
    aq2.open;if aq2.recordcount = 0 then aq1.ExecSQL else showmessage('数据库中已有');
      

  7.   

    这个方法我想到了,不过我的是很复杂的情况,:b,:c,:c的值是根据操作的情况在改变的。这就是让我郁闷的地方啊!
      

  8.   

    天啊!! 你还不如写一个SQL存储过程, 然后返回true or false 来显示是否成功!!
      

  9.   

    我又返回了一下aq1的sql,发现跟aq2的形式是差不多的,所以我估计不是aq2的sql语句的问题了,应该是recordcount的问题了,可就是不知道是哪里出问题来着。
      

  10.   

    我看你代码又不完,正常方法又不用,有点没事找事做.最简单的是,把那几个列设成主键就是了,直接让SQL返回错误!!你只需要插入语句就行了:try
      aq1.execsql
    except
      showmessage('重复')
    end;
      

  11.   

    select 语句用 open方法;
    增删改 语句用execsql方法!
      

  12.   

    一个SQL解决的事,为啥要N多处理了?
    if not exists(select 1 from tablename where field=xxx)
      insert into tablename(...) values(...)