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: