TQuery部件具有一个Params属性,它们在设计时不可用,在程序运行过程中可用,并且是动态建立的,当为TQuery部件编写动态SQL 语句时, Delphi 会自动地建立一个数组Params,数组Params是以0下标开始的,依次对应动态SQL 语句中的参数, 也就是说动态SQL语句中第一个参数对应Params[0],第二个参数对应params[1],依此类推。可在实际运行程序时,
例如:Query1.params[1].AsString := "Lichtenstein";
怎么将设置好的值赋值给SQL中的参数,运行结果就是不对!参与者每人5分,解决问题者100分喽!绝不食言!也可发教程或回复到我邮箱 [email protected]

解决方案 »

  1.   

    with Query1 do
    begin
      SQL.Clear;
      SQL.Add('select * from Table1 where vcField1=:vcParam1 and vcField2=:Param2');
      Params[0].AsString:='aa';
      //也可以ParamByName('vcParam1').AsString:='aa';
      Params[1].AsString:='bb';
      //也可以ParamByName('vcParam2').AsString:='bb';
      Active:=True;
    end;
      

  2.   

    以上是在运行期设置SQL的那一种。
    如果是在设计期就设计好了SQL的那一种:
    with Query1 do
    begin
      Active:=False;//............
      Params[0].AsString:='aa';
      //也可以ParamByName('vcParam1').AsString:='aa';
      Params[1].AsString:='bb';
      //也可以ParamByName('vcParam2').AsString:='bb';
      Active:=True;//..........
    end;
      

  3.   

    with Qurery1 do
    begin
        Close;
        SQL.Clear;
        SQL.Add('select * from Table where field1=:s1 and field2<:s2');
        ParamsByName('S1').AsString:=Edit1.txt;
        ParamsByName(S2').AsInteger:=i;
        Open;
    end;
      

  4.   

    以上的说法我也试过呀,是不是ORDE BY 不能带参数呢?
      

  5.   

    动态设置order by 就不要用参数了,用数据集的Sort属性,如ADODataSet1.Sort := 'Field1';
      

  6.   

    TCustomADODataSet.SortSpecifies the sort order of the recordset.property Sort: WideString;DescriptionSet Sort to establish or change the list of fields on which the recordset is sorted. Set sort to the name of a single field or to a comma-separated list of fields. Each field may optionally be followed by the keyword ASCENDING or DESCENDING to specify a sort direction for the field. If one of these keywords is not used, the default sort direction for the field is ascending. Set Sort to an empty string to reset the recordset to the sort order originally used when the recordset抯 data was first retrieved.ADOQuery1.Sort := 'LastName ASC, DateDue DESC'Read Sort to determine the field (or fields) on which the recordset is sorted.