Delphi TQuery 的Locate用法小探


今天写一个公共部件要用到TQuery.Locate方法。顺便来个小小总结。

Help里的解释

function Locate(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions): Boolean;

KeyFields: is a string containing a semicolon-delimited list of field names on which to search.

KeyValues: is a variant array containing the values to match in the key fields.

TLocateOptions is a set that optionally specifies additional search latitude when searching on string fields.

关于LocateOptions
type
  TLocateOption = (loCaseInsensitive, loPartialKey);
  TLocateOptions = setof TLocateOption;

loCaseInsensitive Key fields and key values are matched without regard to case.//忽略大小写
loPartialKey Key values can include only part of the matching key field value; //只需部分匹配,从头开始匹配
{另外,如果KeyFields里没有字符串类型或者LocateOptions=[],该函数都会忽略此项}

看了这么多,到底Locate是干么的呢?请看

Searches the dataset for a specified record and makes that record the current record.

Call Locate to search a dataset for a specific record and position the cursor on it.

很显然Locate有别于过虑,可不要等同起来哦。Locate主要是在已经Open出来的数据集里定位你要的资料。

好了,看了Help里面密密麻麻的E文,头都晕了,是不是因为头晕所以一直学不好“英国历史(English)”呢?我怎么看E文都是这种感觉,还是中国的方块字看起来亲切舒服,^_^,扯远了,言归正传,看了理论的东西,我们来实践一下。

做事情总是从易处着手,写程序也不例外。我们先看看单栏的用法。

strCode:='Abc'
Tquery1.Locate('code',strcode,[loCaseInsensitive])

这样TQuery,就会将光标定位到code字段中等于Abc(abc,aBc,abC,....)的记录上。

不过,我们既然用来查询大部分情况下我们都是要模糊查询的,所以这样并不是我们要用的方法,我们只要稍微改下Option就OK了。
Tquery1.Locate('code',strcode,[loPartialKey])

这样找到的是这样的结果code中“包含”(是从头开始匹配而不是真正的包含)Abc(Abcddfe...,....)

当然也可以两个同时使用

Tquery1.Locate('code',strcode,[loCaseInsensitive,loPartialKey])

Ok。单栏的查询基本就是这样子了。

哇靠,快6点了,下班下班了.............

明天继续多栏定位....
                                                                                          [ By  浪子(dayichen@163.com)  At:2005.01.06 ]
作者Blog:http://blog.csdn.net/walkingboy/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值