我的一张数据表是这样的: 
表名:info 
字段名:no,no2 
no  no2 
1    0 
1    1 
1    2 
1    3 
2    1 
...  ... 通过sql,把no所对应的数据取出并存入到一维数组中 
例如:no=1 时,对应的no2值为:0,1,2,3;怎样把0,1,2,3依次取出并存入到一维数组中 
我的代码是这样的: 
var 
  i,j,count:integer; 
  arr:array of integer; 
begin 
with ADOQuery1 do 
begin 
sql.Clear; 
sql.Add('select count(*) as count from info where no=''1'''); 
open; 
count:=fieldbyname('count').AsInteger; setlength(arr,count);
sql.Clear; 
sql.Add('select no2 from info where no=''1'''); 
open; 
for i:=0 to count-1 do 
begin 
  arr[i]:=fieldbyname('no2').AsInteger ;//这里应该怎么写才能实现把no2的数据依次
                                                写入到一维数组中,是不是需要 cell之类的,请大虾们指教
end; 
 
 
 

解决方案 »

  1.   

    var
    arr:array of Integer;
    i:Integer;
    begin
    With ADOQuery1 do
    begin
      Close;
      sql.Clear; 
      sql.Add('select * as count from info where no=''1'''); 
      open; 
      SetLength(arr,RecordCount);
      i:=0;
      DisableControls;
      while not Eof do
      begin
        arr[i]:=FieldByName('No2').AsInteger;
        Inc(i);
        Next;
      end;
      EnableControls;
    end;
      

  2.   

    DisableControls;
    EnableControls; 
    这两条语句是什么意思?我不明白
    请大虾简单请解一下
    Thank you !
      

  3.   

    楼上的就可以实现
    不过最好用try..finaly..end; 防止发生意外 
     try   
       adoQuery1.disablecontrols;   
       adoQuery1.close; 
       ......  
       adoQuery1.open;   
     finally   
       adoQuery1.enablecontrols;   
     end;  
      

  4.   

    Delphi的DataSet类可以通过DataSource连到数据感知控件,当DataSet的当前记录位置移动(调用DataSet.Next等方法)时,就会去刷新数据感知控件的内容。
    如果DisableControls,那么在当前记录位置移动时DataSet就不会去刷新感知控件,这样就提高了遍历DataSet的效率。EnableControls就是恢复数据集的“感
    知能力”
      

  5.   

    在执行前,需不需要加上first;这条语句?也就是把指针指向数据集的第一条记录
      

  6.   

    应该改成这样,试试var 
    arr:array of Integer; 
    i:Integer; 
    begin 
    With ADOQuery1 do 
    try
      DisableControls;   Close; 
      sql.Text:='select count(*) from info where no=''1'''; 
      open;   SetLength(arr,Fields[0].AsInteger); 
      Close;
      SQL.Text := 'select no2 from info where no=''1''';///++++
      Open;
      i:=0; 
      while not Eof do 
      begin 
        arr[i]:=FieldByName('No2').AsInteger; 
        Inc(i); 
        Next; 
      end; 
    finally
      EnableControls; 
    end;
      

  7.   

    为什么不需要?难道ADOQUERY有执行时,默认从第一条记录开始?