Tuesday, 4 July 2017

Insert/update and delete query in linq?

Insert :- 

using (DataContext objDataContext = new DataContext())
            {
                Employee objEmp = new Employee();
                // fields to be insert
                objEmp.EmployeeName = "";
                objEmp.EmployeeAge = 25;
                objEmp.EmployeeDesc = "";
                objEmp.EmployeeAddress = "";
                objEmp.EmployeeExperience = 8;
                objDataContext.Employees.InsertOnSubmit(objEmp);
                // executes the commands to implement the changes to the database
                objDataContext.SubmitChanges();
            };

Update :- 

 using (DataContext objDataContext = new DataContext())
            {
                // Get single employee to update
                Employee objEmp = objDataContext.Employees.Single(emp => emp.EmployeeName == "john");
                //Field which will be update
                objEmp.EmployeeExperience = 10;
                // executes the commands to implement the changes to the database
                objDataContext.SubmitChanges();
            };

Deletete :- 

using (DataContext objDataContext = new DataContext())
            {
                // Get single employee to update
                Employee objEmp = objDataContext.Employees.Single(emp => emp.EmployeeName == "john");              
                objDataContext.Employees.DeleteOnSubmit(objEmp);
                // executes the commands to implement the changes to the database
                objDataContext.SubmitChanges();
            };

No comments:

Post a Comment