DesktopApplicationDevelopment LAB 7
DesktopApplicationDevelopment LAB 7
LAB 7
WINDOWS FORM PROGRAMMING DATABASE
CONNECTION
TARGET
✓ Instruct students to familiarize themselves with building Windows App
applications that connect to SQL Server databases.
✓ Design input forms for tables in the database.
✓ Execute basic data queries in the application
✓ In this part, we are going to learn how to perform CRUD operations in a Windows
Forms application using Entity Framework. - List of Entity Framework: EF
Basics, EF 6 DB-First, EF 6 Code-First, EF Core, EF Cheat Sheet, EF Quiz
PRACTICE CONTENT
1. Exercise 1
First, we have to create database and table in SQL Server. Please find the below
scripts for creating the database and table respectively.
DB Script
1. CREATE DATABASE StudentInformation;
2. GO;
Table Script
Connect Database
Add the ADO.NET Entity model in your project. To add, right click your solution
and select Add - > New Item -> Select "Data" in left pane and select "Ado.Net Entity
Model".
At windows “Connection Properties”, We are choose the server name and select
the name of the database defined earlier. In case we don't remember the server name we
can enter the Server name with "."
Please find the below image for your reference.
They can test whether the connection is successful or not by clicking the "Test
Connection" button. Then click OK button, to continue. Continue to press the Next
button. Continue, we choose “Entity Framework 6.x” and click Next button
The next, let us create our custom model class for binding and displaying the values.
Note: Don't forget to declare your class as "public". Then only you can access this class
outside of any other class.
First, add a new folder, then rename "Models". Next, we add a new class and
rename to "StudentInfo".
Code Example: class StudentInfo. Include properties to display:
After defining the "Display" method, at the "Form_Load" event, we call the "Display"
method to execute. Please find the below code for your reference.
Here, we are binding our input values (Name, Age, City, Gender) into
StudentDetails class and passing this to "SaveStudentDetails" method and saving the
entity. Please find the below code for your reference:
Press the F5 key, then enter the information in the box then click the Save button
Update
Next, write the code for update and delete operations. To update the record, we
have to select the record from data GridView. Here, I have written datagridview cell
click event to get the values from datagridview to fields. Please find the below code for
your reference.
We have to select the record from datagridview for update and delete. The same
event will help us to get the records.
Please find the below code for "Update". You define a function that updates data,
which returns the correct value if the update is successful, otherwise false if the update
fails.
After defining the data update function, you will call it in the update button click
event.
Delete
You define a function that delete data, which returns the correct value if the delete
is successful, otherwise false if the delete fails.
After defining the data delete function, you will call it in the delete button click
event. Please find the below code for "Delete".
2. Exercise 2
Same as above, now we do: view, add, delete, edit data across multiple tables.
✓ Design the student information management interface (view, add, delete, edit)
✓ Design interface for managing the list of provinces / cities (view, add, delete,
edit).
✓ Design the class list management interface (view, add, delete, edit).
✓ Update student information into the database. Place of birth and class information
will be taken from two tables: Class and province / city. Show status (success /
failure).
✓ Delete student information by student ID.
✓ Search student information by.
Sample database
Suggestions
Display list of province / city and class in database into combobox. To display data
into the combobox. You write the functions as follows: SetValueCity, SetValueClass.
Then, at the "Form_Load" event, call the two functions SetValueCity and
SetValueClass just defined earlier. Please find the below code for " SetValueCity ".
Next, you create two forms to manage the list of classes and provinces / cities.
Form Manage classroom information
Form Main
To add data for the class table as well as the province/city. You need to open the
information management form of two tables through the corresponding "+" button next
to it. Please find the below code for "+", add Class.
---The end---