3

CRUD Operations In Windows Applications Using C#

 1 year ago
source link: https://www.c-sharpcorner.com/article/crud-operations-in-windows-applications-using-c-sharp/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction

The word CRUD is made up of four words. Those four words are taken from the first letters of the SQL command.

C refers to Create which means to generate a new request or to add something.

R refers to Read which means to display all records.

You refer to Update which means to change the existing records.

D refers to Delete which means to delete existing records.

Step 1

Create a database from MSSQL with the name of the School.

create database school;
use school
create table teacher(id int, Name varchar(50), Address varchar(50),salary int);
select * from teacher;

Table

sqltable.png

Step 2

We create a Windows Application project in C#. Now my project is ready for use.

readyforuse.png

How to use the C# windows Application project

The left-hand side has a toolbox for inserting elements into the windows projects.

Select an element from the toolbox and drop it on the form page.

Each element has properties to change properties according to the requirements.

Double click on any element to redirect its element function.

Step 3

Design a front-end part.

Insert four labels from the toolbox and change the name of labels from the label property text.

Select Label from Toolbox and drop on the Form1 Page.

insertlabel.png

Then change the label name from Label property Text and write a name as ID, Name, Address, Salary.

labelname.png

Step 4

Insert five Text boxes from the toolbox and change the name of Text from the input property name. 

Select Textbox from Toolbox and drop on the From1 Page.

insertinput.png

Then change the name from the textbox property name. We have written the name.

inputname.png

Step 5

Insert six buttons from the toolbox and change the button's name from the button property text and name.

Please select the button from Toolbox and drop it on the Form1 Page.

insertbutton.png

Then change the name of the button as well as the Text of the button from the button property as Insert, Update, Delete, Show All, Find, Exit.

buttonname.png

Step 6

Insert data grid view from the toolbox.

Select data grid view from Toolbox and drop on the Form1 Page

insertdata.png

Step 7

Now My Design is Complete.

formpreview.png

Step 8

Here we start to apply CRUD Operations. To double click on the From1 page and create a SqlConnection and SqlCommand object and in a connection object, we store a connection string, and cmd object we store a SQL command and establish the SQL connection.

SqlConnection conn;
SqlCommand cmd;
private void Form1_Load(object sender, EventArgs e) {
    conn = new SqlConnection(@ "Data Source=LAPTOP-2F8CO37S;Initial Catalog=school;Integrated Security=True");
    cmd = new SqlCommand();
    cmd.Connection = conn;
}

Step 9

Insert

To double-click on the Insert button. Then we write a code under the function to create query variable and in-store the SQL insert command then we define SQL CommandText property equal to query and open the connection and call to ExecuteNonQuery function then close the connection. Here we make another function Clear data for after submitting details to clear the text boxes.

private void btninsert_Click(object sender, EventArgs e) {
    string query = $ "insert into teacher values('{txtid.Text.ToString()}','{txtname.Text}','{txtaddress.Text}','{txtsalary.Text.ToString()}')";
    cmd.CommandText = query;
    conn.Open();
    cmd.ExecuteNonQuery();
    cleardata();
    conn.Close();
    displaydata();
}
private void cleardata() {
    txtid.Clear();
    txtname.Clear();
    txtaddress.Clear();
    txtsalary.Clear();
}

Step 10

Update

To double-click on the Update button. Then we write a code under the function to open the connection and define SQL command type we use command type text and then pass the SQL update command into command text. Then we call to ExecuteNonQuery function. 

private void btnupdate_Click(object sender, EventArgs e) {
    conn.Open();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "update teacher set name='" + txtname.Text + "',Address='" + txtaddress.Text + "',salary='" + txtsalary.Text.ToString() + "' where id='" + txtid.Text.ToString() + "' ";
    cmd.ExecuteNonQuery();
    conn.Close();
    displaydata();
    cleardata();
}

Step 11

Double-click on the Show All button. Here we write a display data function for users to display data into the data grid view. In the display function, we open the connection then create a command and it stores the SqlCommand variable then define command type. Here we use command type text in a commandText. We pass the SQL Select * from teacher command, and call the ExecuteNonQuery function. Then create DataTable and SqlDataAdapter objects and in a SqlDataAdapter object, we give the command object and then call a data table function. Fill and pass the data table object as a parameter then call the DataSouce function from the data grid view then close the connection.

private void btnshow_Click(object sender, EventArgs e) {
    displaydata();
}
private void displaydata() {
    conn.Open();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from teacher";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    conn.Close();
}

Step 12

Delete

Double-click on the Delete button. Then we write a code under the function to create a query variable and that variable we write a SQL delete command and make query equal to Command text then open connection and call the ExecuteNonQuery function then make dataGridView1.DataSource equal to query then closes the connection.

private void btndelete_Click(object sender, EventArgs e) {
    string query = $ "delete teacher where id='{txtid.Text.ToString()}'";
    cmd.CommandText = query;
    conn.Open();
    cmd.ExecuteNonQuery();
    dataGridView1.DataSource = query;
    cleardata();
    conn.Close();
    displaydata();
}

Step 13

Double-click on the Find button. Then we write a code under the function to open the connection and create a command then we write a command to type in command text. We pass the select command then call the ExecuteNonQuery function then create DataTable and SqlDataAdapter object and call the DataAdapter fill function. Then define all text values for display and make a dataGridView1.DataSource is equal to the data table variable then close the connection.

private void btnfind_Click(object sender, EventArgs e) {
    conn.Open();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from teacher where id='" + txtsearch.Text + "'";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    txtname.Text = dt.ToString();
    txtaddress.Text = dt.ToString();
    txtsalary.Text = dt.ToString();
    dataGridView1.DataSource = dt;
    conn.Close();
}

Step 14

Double click on the exit button and write the method Application. Exit for the exit application.

private void btnsave_exit_Click(object sender, EventArgs e) {
    Application.Exit();
}

Output

Animation.gif

Conclusion

Windows application is the easiest method for creating a windows application with uses of the text box, button, label, image, checkbox, grid, and more.

It is simple and time-saving.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK