This is a simple Source Code called CRUD Operation with Search in C# and SQL. This simple prohram will help you to learn how create a C# program that has a Create, Read, Update and Delete features with Search data. The source code itself uses only a single form where the said operation/feature can be done.
The below list are the controls I have used.
I have created a test database in my SQL Server and created a "Register" Table as the storage of the data. The following list are the columns inside the Register Table
.
The image presented below the interface I created for this simple program.
The following scripts/code are ones I have used to manage the data.
Database Connection
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-2LLV29I\MSSQLSERVER01;Initial Catalog=test1;Integrated Security=True");
Save to Database
private void btnsave_Click(object sender, EventArgs e) { if (txtid.Text == "" || txtname.Text == "" || txtsalery.Text == "" || txttax.Text == "" || txtage.Text == "" || comboBox1.Text == "") { MessageBox.Show("please fill the cell first"); } else { conn.Open(); SqlDataAdapter sda = new SqlDataAdapter("insert into Register(id,name,gender,salery,age,tax)values('" + txtid.Text + "','" + txtname.Text + "','" + comboBox1.Text + "','" + txtsalery.Text + "','" + txtage.Text + "','" + txttax.Text + "')", conn); sda.SelectCommand.ExecuteNonQuery(); conn.Close(); MessageBox.Show("data entered succesfully. . . ."); panel1.Enabled = false; } }
View Data
private void load_data() { conn.Open(); sda.Fill(data); dataGridView1.DataSource = data; conn.Close(); }
Update Data
private void btnUpdate_Click(object sender, EventArgs e) { if (panel1.Enabled == true) { if (txtid.Text == "" || txtname.Text == "" || txtsalery.Text == "" || txttax.Text == "" || txtage.Text == "" || comboBox1.Text == "") { MessageBox.Show("please fill the cell first"); } else { conn.Open(); SqlDataAdapter sda = new SqlDataAdapter("UPDATE Register SET id ='" + txtid.Text + "',name = '" + txtname.Text + "',Gender = '" + comboBox1.Text + "', Salery = '" + txtsalery.Text + "',age = '" + txtage.Text + "',tax = '" + txttax.Text + "' where id ='" + txtid.Text + "'", conn); sda.SelectCommand.ExecuteNonQuery(); conn.Close(); MessageBox.Show("data updated succesfully. . . ."); load_data(); panel1.Enabled = false; } } else { MessageBox.Show("please select what you want to update"); } }
Delete Data
private void btndelete_Click(object sender, EventArgs e) { if (panel1.Enabled == true) { if (txtid.Text == "" || txtname.Text == "" || txtsalery.Text == "" || txttax.Text == "" || txtage.Text == "" || comboBox1.Text == "") { MessageBox.Show("please select the record"); } else { conn.Open(); SqlDataAdapter sda = new SqlDataAdapter("delete from Register where id ='" + txtid.Text + "'", conn); sda.SelectCommand.ExecuteNonQuery(); conn.Close(); MessageBox.Show("data deleted succesfully. . . ."); load_data(); panel1.Enabled = false; } } else { MessageBox.Show("please select the record first"); } }
The source code is free to download. Feel Free to download the source code to understand more about how does the program work.
Requirements
Installation/Setup
SqlConnection
according to your database connection string.That's it! I hope this CRUD (Create, Read, Update, and Delete) with Search Program in C# and SQL will help you to develop your programming skills.
Enjoy :)Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.