LINQ to SQL – INSERT
Before writing a LINQ to SQL
query, we first need to create a DataContext using the LINQ to SQL Classes
template, to know more on how to create the DataContext refer to the post LINQ to SQL Sample
Once the DataContext is created
we can query the Object model using LINQ queries, let us consider the Employee
table which has the following structure.
INSERT into a table is a regular database operation, involved in any Transaction processing system.
Assume we have the following data in the
Employee and Department Tables
Table: Employee
ID
|
NAME
|
Phone
|
Email
|
DepartmentID
|
Salery
|
1
|
Tom
|
123-123-1234
|
tom@abcsoftware.com
|
1
|
5000
|
2
|
Harry
|
123-123-1234
|
harry@abcsoftware.com
|
2
|
6000
|
3
|
Peter
|
111-222-3333
|
peter@abcsoftware.com
|
3
|
6500
|
4
|
John
|
111-222-3333
|
john@abcsoftware.com
|
3
|
7500
|
5
|
Charles
|
666-444-2222
|
charles@abcsoftware.com
|
NULL
|
6500
|
Table: Department
ID
|
NAME
|
1
|
Finance
|
2
|
Human Resources
|
3
|
IT
|
4
|
Sales
|
5
|
Marketing
|
The SQL query to insert data into the Employees table will be as follows.
INSERT INTO Employee
(Name, DOB, DOJ, Phone, Email, DepartmentID, Salery)
VALUES ('Robert', '10/07/1973', '05/01/2009', '111-222-3333', 'robert@abcsoftware.com',
Now let us see how the equivalent INSERT query in LINQ Looks like.
INSERT INTO Employee
(Name, DOB, DOJ, Phone, Email, DepartmentID, Salery)
VALUES ('Robert', '10/07/1973', '05/01/2009', '111-222-3333', 'robert@abcsoftware.com',
Now let us see how the equivalent INSERT query in LINQ Looks like.
EmployeeClassesDataContext dbContext
= new EmployeeClassesDataContext();
Employee objEmp = new Employee();
objEmp.Name = "Robert";
objEmp.DOB = "10/07/1973";
objEmp.DOJ = "05/01/2009";
objEmp.Phone = "111-222-3333";
objEmp.Email = "robert@abcsoftware.com";
objEmp.DepartmentID = 4;
objEmp.Salery = 5500;
dbContext.Employees.InsertOnSubmit(objEmp);
dbContext.SubmitChanges();
ID
|
NAME
|
Phone
|
Email
|
DepartmentID
|
Salery
|
1
|
Tom
|
123-123-1234
|
tom@abcsoftware.com
|
1
|
5000
|
2
|
Harry
|
123-123-1234
|
harry@abcsoftware.com
|
2
|
6000
|
3
|
Peter
|
111-222-3333
|
peter@abcsoftware.com
|
3
|
6500
|
4
|
John
|
111-222-3333
|
john@abcsoftware.com
|
3
|
7500
|
5
|
Charles
|
666-444-2222
|
charles@abcsoftware.com
|
NULL
|
6500
|
6
|
Robert
|
111-222-3333
|
robert@abcsoftware.com
|
4
|
5500
|
Related Post
What is LINQ?
LINQ to SQL
LINQ To SQL Vs ADO.Net Entity Framework
LINQ to SQL Sample
LINQ to SQL Select Query with Specific columns
LINQ to SQL Select query with Filter (WHERE Clause)
LINQ to SQL Select Query with Alias Name
LINQ to SQL - Sub Queries
LINQ to SQL – Join 2 Tables
LINQ to SQL – Select DISTINCT Values
LINQ to SQL – Select with SORT (ORDER BY)
LINQ to SQL – Select with AGGREGATE (GROUP BY)
LINQ to SQL – INNER JOIN
LINQ to SQL – LEFT OUTER JOIN
LINQ to SQL – RIGHT OUTER JOIN
LINQ to SQL – INSERT
LINQ to SQL – UPDATE
LINQ to SQL – DELETE
LINQ to SQL – Using ExecuteCommand() to Execute SQL Statements
LINQ to SQL
LINQ To SQL Vs ADO.Net Entity Framework
LINQ to SQL Sample
LINQ to SQL Select Query with Specific columns
LINQ to SQL Select query with Filter (WHERE Clause)
LINQ to SQL Select Query with Alias Name
LINQ to SQL - Sub Queries
LINQ to SQL – Join 2 Tables
LINQ to SQL – Select DISTINCT Values
LINQ to SQL – Select with SORT (ORDER BY)
LINQ to SQL – Select with AGGREGATE (GROUP BY)
LINQ to SQL – INNER JOIN
LINQ to SQL – LEFT OUTER JOIN
LINQ to SQL – RIGHT OUTER JOIN
LINQ to SQL – INSERT
LINQ to SQL – UPDATE
LINQ to SQL – DELETE
LINQ to SQL – Using ExecuteCommand() to Execute SQL Statements
No comments:
Post a Comment