Professional Documents
Culture Documents
PROGRAMMING
Abstract
The practical curriculum of Programming on Windows helps students
master the C # programming language, Windows Form programming.
Provides basic knowledge of visual programming on the Windows
operating system. Basic operations with files, folders, databases,
processes, services, and libraries on Windows. At the end of the course,
students are provided with a full range of knowledge to apply to solve
real problems.
Overview
- 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
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
Table Script
Create a Windows application project in Visual Studio. Please find the below
images for your reference.
Step 1: Create project
After creating the project successfully, we proceed to design the interface. Here, they
have used labels, text-boxes, combo-boxes, Data GridView, and buttons.
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.
At Windows “Entity Data Model Wizard”, We select all the defined tables or
some necessary ones. In the case where the database has "View, Stored Procedures and
Function" then we choose "View" and “Stored Procedures and Function”. After click
Finish button to Complete.
Now, you can see the added Entity Model in your Solution Explorer.
Model Diagram
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".
First, we define a function that retrieves data from the database. Then pour the
data into Data GridView. Please find the below code for your reference.
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.
Click the "Save" button and write the below code. "SaveStudentDetails" is a
method to update the entity. 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
Result:
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".
So we have done the following operations: view, add, delete, and edit data of a
table in the database
Same as above, now we do: view, add, delete, edit data across multiple tables.
Request student information management, student information including: student
number, student's name, date of birth, gender, email, phone number, place of birth
(province / city). Each student belongs to a class.
1. Design the student information management interface (view, add, delete, edit)
2. Design interface for managing the list of provinces / cities (view, add, delete,
edit)
3. Design the class list management interface (view, add, delete, edit)
4. 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)
5. Delete student information by student ID
6. Search student information by code or full name
Sample database