with xszp5_sql do
   begin
    begin
     close();
      SQL.Clear;
      SQL.text:='select * from sqfy';
    open;
    end;
    if xszp5_sql.RecordCount<>0 then
    begin
    for i:=1 to xszp5_sql.RecordCount do 
     begin
      xszp5_sql.RecNo:=i;
       if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-5' then
        edit1.text:=trim(xszp5_sql.FieldByName('sl0').AsString);
        if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-10' then
        edit2.text:=trim(xszp5_sql.FieldByName('sl0').AsString);
     end数据库查明明有两条件记录guig 字段分别为 YSP-5和YSP-10 但就认一条记录,只填S10在edit1
edit2从不填有数据,是循环不对吗????

解决方案 »

  1.   

       if xszp5_sql.RecordCount <>0 then
        begin
        first; //定位到记录第一条
        for i:=1 to xszp5_sql.RecordCount do
        begin
          xszp5_sql.RecNo:=i;
          if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-5' then
            edit1.text:=trim(xszp5_sql.FieldByName('sl0').AsString);
            if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-10' then
            edit2.text:=trim(xszp5_sql.FieldByName('sl0').AsString);
          next; //下一条
        end 
      

  2.   


    with xszp5_sql do 
    begin 
      begin 
        close(); 
        SQL.Clear; 
        SQL.text:='select * from sqfy'; 
        open; 
      end; 
      if xszp5_sql.RecordCount <>0 then 
      begin 
       //  for i:=1 to xszp5_sql.RecordCount do -
        while not Eof do // +
        begin 
          //xszp5_sql.RecNo:=i; -
          if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-5' then 
            edit1.text:=trim(xszp5_sql.FieldByName('sl0').AsString); 
          if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-10' then 
            edit2.text:=trim(xszp5_sql.FieldByName('sl0').AsString); 
          Next;  //+
        end;
      end;
    end;
      

  3.   

    Query循环最好用 Eof,Bof之类的来循环,另外第一次打开不必要用 First
    用RecordCount循环很不准确。
      

  4.   

    最好用Eof,Bof之类的来循环,另外第一次打开不必要用 First 
    2楼tpcnyou说的相当的好。
      

  5.   

    不要使用for循环,改成while 
    with xszp5_sql do 
    begin 
      close(); 
      SQL.Clear; 
      SQL.text:='select * from sqfy'; 
      open; 
      First;
    end; if xszp5_sql.RecordCount <>0 then 
    begin 
      while not xszp5_sql.eof do
      begin 
      //xszp5_sql.RecNo:=i; 
        if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-5' then 
          edit1.text:=trim(xszp5_sql.FieldByName('sl0').AsString);     if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-10' then 
          edit2.text:=trim(xszp5_sql.FieldByName('sl0').AsString); 
        next;
      end;
    end;
      

  6.   

    你的语法应该没错呀,是不是字段名称或者值输入错误了,查不到那条记录。你把查不到的记录用select再查查看看能查到不。
      

  7.   

    with xszp5_sql do
    begin
        close();
        SQL.text:='select * from sqfy';
        open;
        while not eof do
        begin
          xszp5_sql.RecNo:=i;
          if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-5' then
            edit1.text:=trim(xszp5_sql.FieldByName('sl0').AsString);
          if trim(xszp5_sql.FieldByName('guig').AsString)='YSP-10' then
            edit2.text:=trim(xszp5_sql.FieldByName('sl0').AsString);
          next;
        end;
    end;