Perform CRUD Operations in Database Using LINQ

In this article, I explain how to select, insert , update and delete records in a database using LINQ.

The Backend

Step 1

First we need a table for doing the select, insert , update and delete operations. So we need to create a table with the name of "Employees". The following script will create and insert data into the table.

  1. Create table Employees    
  2. (    
  3. ID int primary key identity,    
  4. FirstName nvarchar(50),    
  5. LastName nvarchar(50),    
  6. Gender nvarchar(50),    
  7. Salary int,    
  8. DepartmentId int foreign key references Departments(Id)    
  9. )    
  10.   
  11. Insert into Employees values ('Jai''Reddy''Male', 32000, 1)    
  12. Insert into Employees values ('Ajay''k''Male', 23000, 3)    
  13. Insert into Employees values ('Vijay''j''Male', 8000, 1)    
  14. Insert into Employees values ('Sujay''v''Male', 8000, 2)    
  15. Insert into Employees values ('Kumari''k''Female', 12000, 2)   

In Application (in Visual Studio):

Step 2

Create a new empty ASP.Net web application project and Name it LINQtoSQL.

Step 3

Next go to View -> Open Server Explorer then right-click on Data Connections. It opens a new window.

In that set the Server name, User name, Password and Database then click on "Test Connection" to check whether the connection was established or not.

set Server name

Step 4

Go to Solution Explorer and right-click on your project then select Add New item then select "LINQ to SQL Classes" and name it Sample.dbml.

Sample

Step 5

Open Server Explorer, refresh your databases and drag the "Employees" table in Sample.dbml as shown in the following diagram.

Server Explorer

Step 6

Add a WebForm to your project. Copy the following code inside the Body section.

  1. <div>    
  2.     <asp:Button ID="btnGetData" runat="server" Text="GetData" OnClick="btnGetData_Click" />       
  3.     <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />       
  4.     <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />    
  5.     <asp:Button ID="Delete" runat="server" Text="btnDelete" OnClick="Button4_Click" />    
  6.     <br />    
  7.     <asp:GridView ID="GridView1" runat="server"></asp:GridView>    
  8. </div>  

Step 7

Copy and paste the following code into your code behind file.

  1. using System;    
  2. using System.Linq;    
  3. using System.Web.UI;    
  4. using LINQtoSQL.LinqToSQLFiles;    
  5. namespace LINQtoSQL.Application.Examples    
  6. {    
  7.     public partial class LinqSQL1: System.Web.UI.Page    
  8.     {    
  9.         protected void Page_Load(object sender, EventArgs e)    
  10.         {    
  11.             if (!Page.IsPostBack)    
  12.             {    
  13.                 //GetData();    
  14.             }    
  15.         }    
  16.         private void GetData()    
  17.         {    
  18.             SampleDataContext dbContext = new SampleDataContext();    
  19.             GridView1.DataSource = dbContext.Employees;    
  20.             GridView1.DataBind();    
  21.         }    
  22.         protected void btnGetData_Click(object sender, EventArgs e)    
  23.         {    
  24.             GetData();    
  25.         }   
  26. //Insert   
  27.         protected void btnInsert_Click(object sender, EventArgs e)    
  28.         {    
  29.             using(SampleDataContext dbContext = new SampleDataContext())    
  30.             {    
  31.                 Employee emp = new Employee    
  32.                 {    
  33.                     FirstName = "Jaipal",    
  34.                     LastName = "Reddy",    
  35.                     Gender = "Male",    
  36.                     Salary = 55000,    
  37.                     DepartmentId = 6,    
  38.                 };    
  39.                 dbContext.Employees.InsertOnSubmit(emp);    
  40.                 dbContext.SubmitChanges();    
  41.             }    
  42.             GetData();    
  43.         }   
  44. //Update   
  45.         protected void btnUpdate_Click(object sender, EventArgs e)    
  46.         {    
  47.             using(SampleDataContext dbc = new SampleDataContext())    
  48.             {    
  49.                 Employee emp = dbc.Employees.SingleOrDefault(x = > x.ID == 6);    
  50.                 emp.Salary = 95000;    
  51.                 dbc.SubmitChanges();    
  52.             }    
  53.             GetData();    
  54.         }   
  55. //Delete   
  56.         protected void Button4_Click(object sender, EventArgs e)    
  57.         {    
  58.             using(SampleDataContext dbc = new SampleDataContext())    
  59.             {    
  60.                 Employee emp = dbc.Employees.SingleOrDefault(x = > x.ID == 6);    
  61.                 dbc.Employees.DeleteOnSubmit(emp);    
  62.                 dbc.SubmitChanges();    
  63.             }    
  64.             GetData();    
  65.         }    
  66.     }    
  67. }   
I hope you enjoy this. Please provide your valuable suggestions and feedback if you found this article helpful.


Similar Articles