英文:
Delphi extract records where a field has a specific value
问题
我正在尝试从数据库中获取具有特定值的字段记录。
到目前为止,我已经写了以下内容。
使用 AddressListTable 做
当 AddressListTable.FieldByName('ContactID').AsInteger = I 时
当未达到文件末尾时
所以我想要在 MCListBox 中显示所有具有 ContactID 值为 I 的记录。
目前的情况是它显示了所有未经过筛选的记录。
感谢任何帮助。
英文:
I am trying to get records from a database where a field has a specific value.
Here is what I have written so far.
With AddressListTable do
while AddressListTable.FieldByName('ContactID').AsInteger = I do
while not Eof do
So I would like to display all records that have a ContactID value of I in a MCListBox.
Currently what happens is it shows all the record unfiltered.
Thanks for any help.
答案1
得分: 0
如MBo 所说,最好使用 QueryDataset 并在查询 SQL 代码中执行。如果坚持使用 Table dataset,可以在 AfterOpen 属性中执行。类似以下方式:
procedure TForm1.ADOTable1AfterOpen(DataSet: TDataSet);
var
bm: TBookmark;
MyIntValue: Integer;
begin
bm := DataSet.GetBookmark;
DataSet.DisableControls;
try
DataSet.First;
while not DataSet.Eof do
begin
if DataSet.FieldByName('MyFieldName').AsInteger = MyIntValue then
// 做一些像添加到 Listbox 的操作
else
// 做其他操作
DataSet.Next;
end;
finally
DataSet.GotoBookmark(bm);
DataSet.EnableControls;
end;
end;
英文:
As MBo told it's better to use a QueryDataset and do it in your query sql code. If you insist on doing this with a Table dataset, you can do it on AfterOpen property. Something like this:
procedure TForm1.ADOTable1AfterOpen(DataSet: TDataSet);
var
bm: TBookmark;
MyIntValue: Integer;
begin
bm := DataSet.GetBookmark;
DataSet.DisableControls;
try
DataSet.First;
while not DataSet.Eof do
begin
if DataSet.FieldByName('MyFieldName').AsInteger = MyIntValue then
//do something like add to your Listbox
else
//do something else
DataSet.Next;
end;
finally
DataSet.GotoBookmark(bm);
DataSet.EnableControls;
end;
end;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论