昨晚在Delphi数据库查询中遇到了一个疑惑。代码如下:procedure TFormSearchAchievement.ComboBoxXYChange(Sender: TObject);
var
    XY : string;
begin
     XY := comboboxxy.Items[comboboxxy.Itemindex];
     if not adoconnection1.Connected then
     try
     adoconnection1.Open;
     except
     end;
     adoquery1.Close;
     adoquery1.SQL.Clear;
     adoquery1.SQL.Add('select 专业名称 from Speciality where 学院 =: XY');
     try
        adoquery1.Open;
     except
     end;
     ComboBoxZy.Items.Clear;
     while (not adoquery1.Eof) do
     begin
          ComboBoxzy.Items.Add(adoquery1.FieldValues['专业名称']);
          adoquery1.Next;
     end;end; 运行得到的结果是:  郁闷啊~我查看了,数据库的就是那两个列表名啊!怎回事啊?有谁能告诉我?

解决方案 »

  1.   

    解决了~~!我太不熟悉了~!!
    adoquery1.SQL.Add('select 专业名称 from Speciality where 学院 ='''+XY+'''');
    晕~~
      

  2.   

    procedure TFormSearchAchievement.ComboBoxXYChange(Sender: TObject);
    var
        XY : string;
    begin
        XY := comboboxxy.Items[comboboxxy.Itemindex];
        if not adoconnection1.Connected then
        try
        adoconnection1.Open;
        except
        end;
        adoquery1.Close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select 专业名称 from Speciality where 学院 =: XY');
        try
        adoquery1..Parameters.ParamByName('xy').Value :=你要带入的值 ;
            adoquery1.Open;
        except
        end;
        ComboBoxZy.Items.Clear;
        while (not adoquery1.Eof) do
        begin
              ComboBoxzy.Items.Add(adoquery1.FieldValues['专业名称']);
              adoquery1.Next;
        end;end; ==============看红色部分。你自己用来参数,却不给这个参数赋值并带入,当然会出错。
      

  3.   

    补充一下,你的写法有问题。。比如:try
        adoconnection1.Open;
        except
        end; 这个是错误,即不能打开。那,之后adoquery1.Close;
        adoquery1.SQL.Clear; 
    .......的都将会是错误的。因为。adoconnection 并不能连接,试问,你的 adoquery1 还能操作个什么劲。。下面还有一处,也是同理,,adoquery1 都 open  不成功了,while (not adoquery1.Eof) do  等。还有什么劲。============当然,,,如果你的 adoquery1 不是连接 adoconnection 的,就另当别论,你的前一处不定就是错误;但是,第二个 try 后,就是错误;============当然,这个问题,在数据连接正确时,是不要报错了,。但是假如不成功。就出错了。所以说,你现在这种写法不严谨;你不如这样写:procedure TFormSearchAchievement.ComboBoxXYChange(Sender: TObject);
    var
        XY : string;
    begin
        XY := comboboxxy.Items[comboboxxy.Itemindex];
        if not adoconnection1.Connected then
        try
        adoconnection1.Open;
        except
         exit;    //如果  adoconnection1  open 不成功,后来的代码都忽略了;
        end;
        adoquery1.Close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select 专业名称 from Speciality where 学院 =: XY');
        try
        adoquery1..Parameters.ParamByName('xy').Value :=你要带入的值 ;
            adoquery1.Open;
        except
         exit;    //如果 adoquery1  open 不成功,后来的代码都忽略了;
        end;
        ComboBoxZy.Items.Clear;
        while (not adoquery1.Eof) do
        begin
              ComboBoxzy.Items.Add(adoquery1.FieldValues['专业名称']);
              adoquery1.Next;
        end;end; 
      

  4.   

    严重支持楼上的,虽然自己是delphi菜菜,但是坚持从最基本的做总没有错~