提取具有特定数值的字段的Delphi记录。

huangapple go评论54阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2023年4月11日 13:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定