Recent

Author Topic: Data Aware Components: When & why to use them?  (Read 552 times)

Raul_ES

  • Full Member
  • ***
  • Posts: 178
  • My interests: Healthcare & Computers
    • My Linkedin Profile, you can add me to stay in contact.
Data Aware Components: When & why to use them?
« on: August 14, 2020, 06:18:25 pm »
Hello,


I am really new in database programming and I have basic skills from the CS degree (all sql).  I am used to grab data from a database (dbase for its simplicity) and put it in a string grid, edit box, whatever is expected to be placed. Just a few newbie questions:

What are the benefits of using data aware components? What actually means a component to be "data aware" capable?


Why I should use them? When?


It is always advisable to use them? There is any particular situation where we shall avoid using them?


Thank You,



Pharmacy + Chemistry + Biology + Healthcare + Computing

If you have any interest or project related with these areas feel free to contact me!

wp

  • Hero Member
  • *****
  • Posts: 7737
Re: Data Aware Components: When & why to use them?
« Reply #1 on: August 14, 2020, 06:59:42 pm »
Basically, you can create an application without writing a single line of code (well - in practice I'd still recommend to type a few lines... -- see below):
  • Put a TDbf component on the form (on palette "Data Access"). Type the (absolute) path to the dbf file into property "FilePathFull" (without filename) and the name of the dbf file (without path) into property "TableName".
  • Add a TDatasource component on the form (on palette "Data Access"). Link its "Dataset" property to the TDbf component of the previous step
  • Add a TDBGrid component on the form (on palette "Data Controls") and client-align it to the form. Link its "DataSource" property to the TDataSoruce component of the previous step
  • Return to the TDbf component. Set its property "Active" to true - voila: you see the contents of the dbf file at designtime.
  • Run the application: you can scroll through the data, edit cells, add new records (scroll below the last row), delete record (press CTRL-DEL)
OK - I don't want to give a database tutorial here. Just one remark, though: Your application will fail to run and even to load when the dbf file is not found at the location you specified. It is highly recommended to turn off the "Active" property of the component after the first tests and to turn it on only at runtime by code, e.g. in the OnCreate event of the form:

Code: Text  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   if FileExists(Dbf1.FilePathFull + Dbf1.TableName) then
  4.     Dbf1.Active := true
  5.   else
  6.     ShowMessage('Database file not found.');

Hopefully this gets you started.

Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

mangakissa

  • Hero Member
  • *****
  • Posts: 1101
Re: Data Aware Components: When & why to use them?
« Reply #2 on: August 18, 2020, 09:21:30 am »
If you want fully control of your components then dbcomponents are not the way. The most ORM applications doesn't use dbcomponents. I have managed (for my self) to create a basic tool thats fills data in an TEdit, TCombobox, TCheckbox, TListview and TStringlist. But it is a lot of work and I notised it's a lot faster than dbcomponents.

Just like WP said: they are easy to use with a few components on form. The datalink is doing the trick for you showing data in your components. 90% of my applications running on dbcomponents.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Raul_ES

  • Full Member
  • ***
  • Posts: 178
  • My interests: Healthcare & Computers
    • My Linkedin Profile, you can add me to stay in contact.
Re: Data Aware Components: When & why to use them?
« Reply #3 on: August 22, 2020, 11:21:15 am »
Thank you for your comments wp and magakissa,


regards

Pharmacy + Chemistry + Biology + Healthcare + Computing

If you have any interest or project related with these areas feel free to contact me!

 

TinyPortal © 2005-2018