动态SQL语句编程的问题,在CSDN的处女贴!

luokING 2004-02-02 05:25:09
TQuery部件具有一个Params属性,它们在设计时不可用,在程序运行过程中可用,并且是动态建立的,当为TQuery部件编写动态SQL 语句时, Delphi 会自动地建立一个数组Params,数组Params是以0下标开始的,依次对应动态SQL 语句中的参数, 也就是说动态SQL语句中第一个参数对应Params[0],第二个参数对应params[1],依此类推。

可在实际运行程序时,
例如:Query1.params[1].AsString := "Lichtenstein";
怎么将设置好的值赋值给SQL中的参数,运行结果就是不对!

参与者每人5分,解决问题者100分喽!绝不食言!

也可发教程或回复到我邮箱 LK2765@163.COM
...全文
33 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
luokING 2004-02-02
  • 打赏
  • 举报
回复
明白了谢谢大家!
hthunter 2004-02-02
  • 打赏
  • 举报
回复
TCustomADODataSet.Sort

Specifies the sort order of the recordset.

property Sort: WideString;

Description

Set 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.
hthunter 2004-02-02
  • 打赏
  • 举报
回复
动态设置order by 就不要用参数了,用数据集的Sort属性,如ADODataSet1.Sort := 'Field1';
luokING 2004-02-02
  • 打赏
  • 举报
回复
以上的说法我也试过呀,是不是ORDE BY 不能带参数呢?
junmayang 2004-02-02
  • 打赏
  • 举报
回复
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;
zhoutian618 2004-02-02
  • 打赏
  • 举报
回复
以上是在运行期设置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;
zhoutian618 2004-02-02
  • 打赏
  • 举报
回复
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;
zhoutian618 2004-02-02
  • 打赏
  • 举报
回复
处女贴?
先顶!

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧