In PART 1 of this post I presented how to start creating a simple application with a small database in Visual Studio and SQL Server. Now I would like to continue by adding some new features to our sample application.
- As presented in Part 1, the app will store the Extrahours worked by company’s employees. After the users are adding these hours, they would like to be able to visualize in the graphic interface the hours from the database without having to query the database in SQL Server.
Considering this, we can add to our form a DataGridView and link it to the SQL Server database to display the information that we want to see.
Step 1: Add the DataGridView to Form1 and name it: dataGridView1:
Step 2: Add 2 buttons: button2 with text “All Records” and button3 with text “Summarization”. The Form1 will look like this:
Step 3: Add new C# methods in order to link the DataGridView and the new 2 buttons to the SQL DataBase. My new code is the following:
Step 4: Run the App and see in the DataGridView All database Records or a Summarization by Name, Year and Month, by pressing the corresponding buttons. The DataGridView is also filled and refreshed when the application starts or after a new record is added:
- The SQL simple Database looks like this:
Of course, if we need, we can add more fields, tables or features and create a “real app”, but I think this is a good start which presents a few methods that we can use to link the Windows Forms interface to a SQL Server Database.