+ -
当前位置:首页 → 问答吧 → 动态创建ADO组件

动态创建ADO组件

时间:2011-10-25

来源:互联网

var
  Madoquery :array of Tadoquery;
  a,b,i,n:integer;
begin
  i:=4;
  for a:=1 to i do
  begin
  Madoquery[a]:=Tadoquery.Create(self);
  Madoquery[a].Connection:=mainform.ADOConnection1;
  with Madoquery[a] do
  begin
  close;
  sql.Clear;
  sql.Add('select 月,count(月) as 数字 from t_2011');
  sql.Add(' where 前三位='+inttostr(n)+' and 日期>=#'+time1+'#');
  sql.Add(' and 日期<=#'+time2+'# group by 月');
  showmessage(sql.Text);
  open;
  end
  end;

运行报内存错误,有没有高人帮忙解答一下,谢谢

作者: liiu83724   发布时间: 2011-10-25

Delphi(Pascal) code

var
  Madoquery :array of Tadoquery;



  应该是这样吧,你没有分配内存,只是声明而已,试下Madoquery :array [0..4]of Tadoquery;

  (可能说错了)

作者: moshao6   发布时间: 2011-10-25

LS没说错,动态数组是需要SetLength声明才能使用的
另外LZ的代码没释放创建的adoquery

作者: funxu   发布时间: 2011-10-25

Delphi(Pascal) code
var
  Madoquery :array of Tadoquery;
  a,b,i,n:integer;
begin
  i:=4;
  SetLength(mADOQuery,i);//设置动态数组长度
  for a:=1 to i do
  begin
    Madoquery[a]:=Tadoquery.Create(self);
    Madoquery[a].Connection:=mainform.ADOConnection1;
    with Madoquery[a] do
    begin
      close;
      sql.Clear;
      sql.Add('select 月,count(月) as 数字 from t_2011');
      sql.Add(' where 前三位='+inttostr(n)+' and 日期>=#'+time1+'#');
      sql.Add(' and 日期<=#'+time2+'# group by 月');
      showmessage(sql.Text);
      open;
    end
  end;


作者: wooden954   发布时间: 2011-10-25

Delphi(Pascal) code
begin
  i:=4;
  setlength(Madoquery,i+1);//要初始化数组!(下面的语句使用了4的下标,所以i+1)
  for a:=1 to i do begin
    Madoquery[a]:=Tadoquery.Create(self);
    Madoquery[a].Connection:=ADOConnection1;
    with Madoquery[a] do begin
      close;
      sql.Clear;
      sql.Add('select 月,count(月) as 数字 from t_2011');
      sql.Add(' where 前三位='+inttostr(n)+' and 日期>=#'+time1+'#');
      sql.Add(' and 日期<=#'+time2+'# group by 月');
      showmessage(sql.Text);
      open;
    end
  end;
end;

作者: D_Parent   发布时间: 2011-10-25