You are on page 1of 8

CURD

Create - Update - Retrieve - Delete


Library to be added
using System.Data.SqlClient;
To be added in the button code.
• SqlConnection con = new
SqlConnection(@"C:\Users\Administrator\AppData\Local\Microsoft\
Microsoft SQL Server Local
DB\Instances\Projects\master.mdf;Integrated Security=True;Connect
Timeout=30");
• The path can be accessed from data.mdf (connection string)
Insert Button
• con.Open();
• SqlCommand cmd = con.CreateCommand();
• cmd.CommandType = CommandType.Text;
• cmd.CommandText= "insert into StdData values
('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";
• cmd.ExecuteNonQuery();
• con.Close();
• MessageBox.Show("Record Inserted Successfully");
Create new class
• public void disp_data()
• {
• con.Open();
• SqlCommand cmd = con.CreateCommand();
• cmd.CommandType = CommandType.Text;
• cmd.CommandText = "select * from StdData";
• cmd.ExecuteNonQuery();
• DataTable dt = new DataTable();
• SqlDataAdapter da = new SqlDataAdapter(cmd);
• da.Fill(dt);
• dataGridView1.DataSource = dt;
• con.Close();
• }
On form upload
• private void Form1_Load(object sender, EventArgs e)
• {
• data_display();
• }
Delete Button
• con.Open();
• SqlCommand cmd = con.CreateCommand();
• cmd.CommandType = CommandType.Text;
• cmd.CommandText = "delete from StdData where
FName='"+textBox1.Text+"'";
• cmd.ExecuteNonQuery();
• con.Close();
• disp_data();
• MessageBox.Show("Record deleted Successfully");
Update Button
• con.Open();
• SqlCommand cmd = con.CreateCommand();
• cmd.CommandType = CommandType.Text;
• cmd.CommandText = “update StdData set
FName='"+textBox2.Text+“’ where RollNo= ='"+textBox1.Text+“’ ";
• cmd.ExecuteNonQuery();
• con.Close();
• disp_data();
• MessageBox.Show("Record updated Successfully");

You might also like