Note that without writing more complex code, the application could not do much. So don't expect to be able to write a real world database application without writing codes. However the understanding of how to do things in design time (visually) will greatly reduce your coding time.
Let's start by explaining the basic scheme of database handling in Delphi. In Delphi's database framework, you will need 3 kinds of components to work with a database.
The first kind of component you need is dataset component. Dataset component is components that provides means to operate on data. For example, you need dataset component to retrieve data from database, you also need dataset component to insert new data, update data, and delete data. The example of such components includes: (in BDE pallette) TTable, TQuery, (in ADO pallette) TADODataset, TADOQuery, and TADOTable.
The second kind component you need is component that provides connection to the database. This component allows to define a single point of connection that can be used by several dataset. With this component, you don't have to specify the database settings (such as db location, db name, and db login information) for each dataset you have. Examples of this component are: (in BDE pallette) TDatabase, and (in ADO pallette) TADOConnection.
The last but not the least component you need is data controls. These components are what you need to show data to users and also to capture inputs. They are what users have to use to work on their data. Examples of data controls: TDBEdit (to handle data in single line), TDBMemo (to handle multiple lines of data), TDBText (to show static data, behaves like TLabel), TDBImage, TDBListBox (to show list of options), and TDBGrid (to show multiple records in table-like control).
Our First Database Application
Let's start building our first database application. This one is a really simple one for its purpose is just to introduce you to the basic. So in this project we only build an application to show the content of a table.
For this project we will use a database created with MS Access. The sample database is attached with this tutorial, feel free to download it first (see link in the end of this tutorial). The name of the database is db.mdb. The database only contains a table with name Table1 with structure of:
ID : Integer, autoincrement; FirstName : String, max 40 chars; LastName : String, max 40 chars; Note : String, max 255 chars;
1. Create new project.
In Delphi IDE create new project by open menu File and select menu item New - Application. You will get new empty form. Save the application and the form. Let's give the application name HelloDb, and give the form name of FormMain and save it as Form_Main.pas. Don't forget to copy our database (db.mdb) to the same location of the project file.
2. Drop and configures TADOConnection
To connect with MS Access database we need TADOConnection. So click on ADO pallette, click on ADOConnection icon, and then click on the main form. Now we have a fresh TADOConnection in our main form. Name it MainConnection. Time to configure it to connect to our database.
To configure TADOConnection in design time, just double click it. A configuration dialog similar with the image below will show.
For this project we want to provide database settings using connection string For it's easier to do. So click the Build button. You will be presented with a dialog with four tabs, like shown in the following image.
In the Provider tab, make sure you select Microsoft Jet 4.0. OLE DB Provider. Like shown above. It's the database driver that our TADOConnection will use to connect with our database.
In the Connection tab, you have to supply the database name and location. Use the ellipsis button to browse for the database. After you have selected the correct db file you can test whether you have provided correct information or not by clicking on Test connection button. Ignore the other fields in this tab and also ignore the other tabs for they are not relevant for our current project.
Click OK to close this dialog and return to our connection string dialog. See that now our connection string has been filled nicely. Click OK button to save the new connection string and close the dialog.
Set MainConnection's LoginPrompt property to False, since we are not required to supply login credentials when we are accessing this database.
3. Drop A Dataset (TADOTable)
We have setting up our connection correctly, now time to add the first dataset. Click on ADOTable icon on your component pallette (tab ADO) and click again somewhere in your form. Now you have a fresh TADOTable. Name it Table1 for easy reference. Adjust it to use MainConnection by setting its Connection property to MainConnection (Object Inspector provides you with a list, so just select from the list). Now time to tell it which table it should handle. Do it by setting its TableName property into Table1. Again the object inspector will provide you with a list of known tables in the database. So just select from the list.
After done all the above, make sure that Table1 is active by setting its Active property is set to True.
4. Show The Content
Now time to show the content of Table1. For this we need a TDataSource component and a few data controls. TDataSource is required to connect our data controls to our dataset. Open pallette Data Access, click on DataSource icon, and then click somewhere on your form. There you get your fresh TDataSource. Name it Table1Source. Select Table1 for its Dataset property and it's ready.
We want to show the whole content of Table1, so our best choice is to add a TDBGrid to our form. To add TDBGrid, open pallette Data Controls, click on DBGrid icon, then click on your form. Now you have a fresh TDBGrid. Name it grdTable1. Adjust its size and location just like shown in the following image.
To connect the TDBGrid to our Table1, all you have to do is to connect it to the TDataSource we created before, Table1Source. So using object inspector set its DataSource property to Table1Source. If you Table1 is active, the DBGrid will immediately filled with data. If not, just activate Table1 (set its Active property to True).
Now you can play around with the DBGrid in design time, like adjusting its size or the width of each column.
5. Run Your First DB Application
Now are you set? Let's run our first db application. Press F9 shortcut key and see your project compiles and run. See, it's easy to create db application with Delphi. Now you can play around with properties of MainConnection, Table1, Table1Source, and grdTable1 to learn how they will affect the project.
Full source code of the demo project and the sample database is attached. Feel free to use or improve it. Any kind of feedback or question will be appreciated.
Stay tune, because more tutorials on database with Delphi will follow! Next tutorial would be improving the demo project to be capable of adding new record/data.