1. Open the new Project.
2. Place the labels and rename shown in figure, textboxes and buttons.
3. Create connection in form load function:
SqlConnection con = new SqlConnection(@"Data Source=HIMANSHU-PC;Initial Catalog=Emp;Integrated Security=True");
SqlCommand cmd;
INSERT BUTTON CODE
4. Double click on the insert button and write the code as follows:
cmd = new SqlCommand("insert into Emp Values(@id,@name,@city,@sal,@dig,@did) ", con);
con.Open();
SqlParameter p1 = new SqlParameter("@id", SqlDbType.Int);
SqlParameter p2 = new SqlParameter("@name", SqlDbType.VarChar, 20);
SqlParameter p3 = new SqlParameter("@city", SqlDbType.VarChar, 20);
SqlParameter p4 = new SqlParameter("@sal", SqlDbType.Decimal);
SqlParameter p5 = new SqlParameter("@dig", SqlDbType.VarChar, 20);
SqlParameter p6 = new SqlParameter("@did", SqlDbType.Int);
p1.Value = textBox1.Text;
p2.Value = textBox2.Text;
p3.Value = textBox3.Text;
p4.Value = textBox4.Text;
p5.Value = textBox5.Text;
p6.Value = textBox6.Text;
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
cmd.Parameters.Add(p5);
cmd.Parameters.Add(p6);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("record insert");
}
else
{
MessageBox.Show("not insert");
}
con.Close();
UPDATE BUTTON CODE
5. Double Click on the Update Button and write code as follows:
cmd = new SqlCommand("Update Emp SET name=@Name"+","+ "city=@City, sal=@Sal"+","+"dig=@Dig ,did=@Did where id=@Id", con);
con.Open();
cmd.Parameters.AddWithValue("@Id", textBox1.Text);
cmd.Parameters.AddWithValue("@Name", textBox2.Text);
cmd.Parameters.AddWithValue("@City", textBox3.Text);
cmd.Parameters.AddWithValue("@Sal", textBox4.Text);
cmd.Parameters.AddWithValue("@Dig", textBox5.Text);
cmd.Parameters.AddWithValue("@Did", textBox6.Text);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Record Update");
}
else
{
MessageBox.Show("Record Not Update ");
}
con.Close();
DELETE BUTTON CODE
6. Double Click on the Delete Button and write code as follows:
cmd = new SqlCommand("delete from Emp where id=@Id",con);
con.Open();
cmd.Parameters.AddWithValue("@ID", textBox1.Text);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("record delete");
}
else
{
MessageBox.Show("Not delete");
}
con.Close();
SELECT BUTTON CODE
7. Double Click on the Select Button and write code as follows:
cmd = new SqlCommand("SELECT * FROM Emp WHERE id=@Id", con);
con.Open();
cmd.Parameters.AddWithValue("@Id", textBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox2.Text = dr[1].ToString();
textBox3.Text = dr[2].ToString();
textBox4.Text = dr[3].ToString();
textBox5.Text = dr[4].ToString();
textBox6.Text = dr[5].ToString();
}
else
{
MessageBox.Show("No Record Found");
}
con.Close();
No comments:
Post a Comment