VR Indian Wanderers Youtube Facebook RSS Feed
Learn with Vikas Suhag
Design an application to perform insertion ,Deletion and Updation in Database(Sql Server)
Nov 16, 2021   Vikas Suhag

SQL Insert, update and delete using C#

AIM:Design an application to perform insertion ,Deletion and Updation in Database(Sql Server).

Source Code :

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

// Insertion

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                sqlConnection1.Open();

                sqlCommand1.Connection = sqlConnection1;

                sqlCommand1.CommandText = "insert into student values('" + textBox1.Text + "','" + textBox2.Text+ "','" + comboBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

                sqlCommand1.ExecuteNonQuery();

                MessageBox.Show("Data Inserted");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            sqlConnection1.Close();

        }

// Updation

        private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                sqlConnection1.Open();

                sqlCommand1.Connection = sqlConnection1;

                sqlCommand1.CommandText = "update student set Address='"+textBox4.Text+"' where (Roll='"+textBox1.Text+"')";    

                sqlCommand1.ExecuteNonQuery();

                MessageBox.Show("Data Updated");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            sqlConnection1.Close();

        }

// Deletion

        private void button3_Click(object sender, EventArgs e)

        {

            try

            {

                sqlConnection1.Open();

                sqlCommand1.Connection = sqlConnection1;

                sqlCommand1.CommandText = "delete from student where (Roll='"+textBox1.Text+"')";

                sqlCommand1.ExecuteNonQuery();

                MessageBox.Show("Data Deleted");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            sqlConnection1.Close();

        }

    }

}

Output:


Leave a Comment

* Email will not be published.
All comments are only visible after approval.
Most Viewed articles
Travel Blog - VR Indian Wanderers
VR Indian Wanderers
Govt. Jobs Portal
Govt. Jobs Portal
Free Chart Maker
Make free animated Charts from .CSV
Search
Youtube Channel
Podcast
Subscribe to Email Updates
Connect With Us
VR Indian Wanderers Youtube Facebook RSS Feed
© 2024 Learn with Vikas Suhag. All Rights Reserved.