Program to Insert,Update and Delete Records by using Parameterized Query.


 
Design:-




using System.Data.SqlClient;
namespace FormNPQInsert
{
   
    public partial class FormIUDPQ : Form
    {
       SqlConnection Con = new     SqlConnection("Server=.;UserId=sa;
Password=Admin123;DataBase=Employee");
        SqlCommand Cmd;
        public FormIUDPQ()
        {
            InitializeComponent();
        }

      private void btnInsert_Click(object sender, EventArgs e)
        {
            string q="insert into EmpDetails values
(@PEmpId,@PEName,@PDoj,@PSalary)";
            Cmd = new SqlCommand(q,Con);
            Cmd.CommandType = CommandType.Text;
            Cmd.Parameters.AddWithValue("@PEmpId",txtEmpId.Text);
            Cmd.Parameters.AddWithValue("@PEName",txtEName.Text);
            Cmd.Parameters.AddWithValue("@PDoj",txtDoj.Text);
            Cmd.Parameters.AddWithValue("@PSalary",txtSalary.Text);
            Con.Open();
            int r = Cmd.ExecuteNonQuery();
            Con.Close();
            MessageBox.Show(r+"  Record(s) Inserted Successfully");

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string q = "Update EmpDetails set EName=@PEName,Doj=@PDoj,
Salary=@PSalary where  EmpId=@PEmpId";
            Cmd = new SqlCommand(q,Con);
            Cmd.CommandType = CommandType.Text;
            Cmd.Parameters.AddWithValue("@PEmpId",txtEmpId.Text);
            Cmd.Parameters.AddWithValue("@PEName", txtEName.Text);
            Cmd.Parameters.AddWithValue("@PDoj", txtDoj.Text);
            Cmd.Parameters.AddWithValue("@PSalary", txtSalary.Text);
            Con.Open();
            int r = Cmd.ExecuteNonQuery();
            Con.Close();
            MessageBox.Show(r+ "  Record(s) Updated Successfully");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string q = "Delete EmpDetails where EmpId="+txtEmpId.Text+"";
            Cmd = new SqlCommand(q,Con);
            Cmd.CommandType = CommandType.Text;
            Cmd.Parameters.AddWithValue("@PEmpId",txtEmpId.Text);
            Con.Open();
            int r = Cmd.ExecuteNonQuery();
            Con.Close();
            MessageBox.Show(r+"  Record(s) Deleted Succcessfully");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (Control x in this.Controls)
                if (x is TextBox)
                    x.Text = "";
            MessageBox.Show("TextBoxes Are Cleared");
        }
    }
}



No comments:

Post a Comment