高手请教,我的程序在FormShow中的执行,我需要根据ComboBox1的Text变化选择查询显示相应的数据值可是只能显示一条在变换ComboBox1的Text的内容,其他的Edit.text没有任何变化!!郁闷!!!!谢谢指教!!!vara : string;
begin
    Edit9.Text := Form1.ComboBox1.Text;
    DS := TSQLDataset.Create(nil);
    DS.SQLConnection := SQLConnection1;
    DS.CommandType := ctQuery;
    DS.CommandText :='select * from cpdj';
  try
    DS.Open;
    while not DS.Eof  do
    begin
      ComboBox1.Items.Add(DS.FieldByName('cname').AsString);
      ComboBox1.ItemIndex := 0;
      DS.Next;
      end;
    begin
       a := ComboBox1.Text;
       DS := TSQLDataset.Create(nil);
       DS.SQLConnection := SQLConnection1;
       DS.CommandType := ctQuery;
       DS.CommandText :='select cid from cpdj where cname='+quotedstr(a)+'';
       DS.Open;
      while not DS.Eof  do
       begin
       edit2.Text :=DS.FieldByName('cid').AsString ;
       ds.Next;
       end;
        DS.Close;
        DS.CommandText :='select clx from cpdj where cname='+quotedstr(a)+'';
        DS.Open;
        while not DS.Eof  do
        begin
        edit3.Text :=DS.FieldByName('clx').AsString ;
        ds.Next;
        end;
       DS.Close;
       DS.CommandText :='select cxh from cpdj where cname='+quotedstr(a)+'';
       DS.Open;
       while not DS.Eof  do
       begin
       edit4.Text :=DS.FieldByName('cxh').AsString ;
       ds.Next;
       end;
       DS.Close;
       DS.CommandText :='select ghsname from cpdj where cname='+quotedstr(a)+'';
       DS.Open;
       while not DS.Eof  do
       begin
       edit4.Text :=DS.FieldByName('ghsname').AsString ;
       end;
       end;
     finally
    DS.Free;  //当数据库操作完成后释放。
  end;
  begin
  self.DateTimePicker1.datetime :=now;
  end;
end;

解决方案 »

  1.   

    你这个程序有几个问题:你动态创建了DS两次。还有你只是取一条记录的值,为什么每次都用WHILE NOT DS。Eof DO 的循环。还有,你需要判断一下你检索出来的数据是否不为空 ,使用 DS。IsEmpty 进行判断。
      

  2.   

    事件要放到ComboBox1Change中写
            while not DS.Eof  do
            begin
            edit3.Text :=DS.FieldByName('clx').AsString ;  //这样只显示查询出来的结果集的最后一条记录
            ds.Next;
            end;
      

  3.   

    天气太热,代码看的头晕你在form上好好放几个数据库控件就那么占地方吗?非要写一堆create..:=...free