今天写一个公共部件要用到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/