Simple SELECT, INSERT, UPDATE and DELETE Using LINQ to SQL

Language-INtegrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. In other words LINQ has the power of querying on any source of data (Collection of objects, database tables or XML Files). We can easily retrieve data from any object that implements the IEnumerable<T> interface and any provider that implements the IQueryable<T> interface.

Microsoft basically divides LINQ into the following three areas:

  • LINQ to Object : Queries performed against in-memory data
  • LINQ to ADO.Net
    • LINQ to SQL (formerly DLinq) : Queries performed against the relation database; only Microsoft SQL Server is supported.
    • LINQ to DataSet : Supports queries by using ADO.NET data sets and data tables.
    • LINQ to Entities : Microsoft ORM solution
  • LINQ to XML (formerly XLinq) : Queries performed against the XML source.

LINQ-to-SQL.gif

 

LINQ to SQL


LINQ to SQL translates our actions to SQL and submits the changes to the database. Here we will perform Select, Insert, Update and Delete operations on a COURSE table.

Step 1: Create a COURSE Table in the database

LINQ-to-SQL-class.gif

Step 2: Create a ContextData file using the Object Relational Designer:

Create a new item, select the LINQ to SQL classes (as shown in the following figure) and name it Operation.dbml.

LINQ-to-SQL-class1.gif

After clicking the Add button the ContextData file is created. Now we should drag all the tables onto the left-hand side of the designer and save (as shown in the following figure). This will create all the mappings and settings for each table and their entities.

LINQ-to-SQL-class2.gif

For .dbml files the database connection string is defined in the web.config file as:

  1. <connectionStrings>  
  2. <add name="DevelopmentConnectionString" connectionString="Data Source=sandeepss-PC;Initial Catalog=Development;User ID=sa;  
  3. Password=*******" providerName="System.Data.SqlClient" />  
  4.  </connectionStrings> 
We can use a connection string from the web.config file or we can pass a connection string as a parameter in the constructor of the DataContext class to create an object of the DataContext class.

The SELECT Operation
  1. private void GetCourses()  
  2. {  
  3.       //create DataContext object  
  4.       OperationDataContext OdContext = new OperationDataContext();  
  5.       var courseTable = from course in OdContext.GetTable<COURSE>() select course;  
  6.       //grdCourse is gridview id  
  7.       grdCourse.DataSource = courseTable;  
  8.       grdCourse.DataBind();  
  9. }
The INSERT Operation
  1. private void AddNewCourse()  
  2. {  
  3.       //Data maping object to our database  
  4.       OperationDataContext OdContext = new OperationDataContext();  
  5.       COURSE objCourse = new COURSE();  
  6.       objCourse.course_name = "B.Tech";  
  7.       objCourse.course_desc = "Bachelor Of Technology";  
  8.       objCourse.modified_date = DateTime.Now;  
  9.       //Adds an entity in a pending insert state to this System.Data.Linq.Table<TEntity>and parameter is the entity which to be added  
  10.       OdContext.COURSEs.InsertOnSubmit(objCourse);  
  11.       // executes the appropriate commands to implement the changes to the database  
  12.       OdContext.SubmitChanges();  
  13. }
The Update Operation
  1. private void UpdateCourse()  
  2. {  
  3.       OperationDataContext OdContext = new OperationDataContext();  
  4.       //Get Single course which need to update  
  5.       COURSE objCourse = OdContext.COURSEs.Single(course => course.course_name == "B.Tech");  
  6.       //Field which will be update  
  7.       objCourse.course_desc = "Bachelor of Technology";  
  8.       // executes the appropriate commands to implement the changes to the database  
  9.       OdContext.SubmitChanges();  
  10. }
The DELETE Operation
  1. private void DeleteCourse()  
  2. {  
  3.       OperationDataContext OdContext = new OperationDataContext();  
  4.       //Get Single course which need to Delete  
  5.       COURSE objCourse = OdContext.COURSEs.Single(course => course.course_name == "B.Tech");  
  6.       //Puts an entity from this table into a pending delete state and parameter is the entity which to be deleted.  
  7.       OdContext.COURSEs.DeleteOnSubmit(objCourse);  
  8.       // executes the appropriate commands to implement the changes to the database  
  9.       OdContext.SubmitChanges();  
  10. }
Conculsion

To perform select, insert, update and delete operations we create a table and create a data context class; in other words a dbml file. In this file designer view we drag and drop the COURSE table from the Server Explorer. This data context class is an Object and table mapping and we perform the operation on the object and database updated according to the action using the submitChanges() method.


Similar Articles