简体   繁体   中英

TADOTable - how to count specific records at once?

My DB Table (Customer) has a field "Active" (YesNo). I'm using TADOTable to work with this table and I would like to have a statistic in the status bar of my app saying how many customers are active and how many are not active. I can always read if my current customer is active by writing this:

bool isActive = CustomerADOTable->FieldByName("Active")->AsBoolean;

but, how to check all records at once? Or do I really need to use TADOQuery and sql statements only because of this?

You're either going to have to run a query, or loop through all records in the table and keep a count of how many have Active set to true.

The query is going to be significantly faster, unless you've got very few records. And the query is the right way(tm) to do it. It'll scale much better.

To avoid looping and an own query, you might use the Filter property.

begin
  Showmessage(IntToStr(DS.RecordCount));
  DS.Filter := 'Active=true';
  DS.Filtered := true;
  Showmessage(IntToStr(DS.RecordCount));
  DS.Filtered := false;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM