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.

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

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.

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

privatevoid AddNewCourse(){//Data maping object to our databaseOperationDataContext OdContext = newOperationDataContext();COURSE objCourse = newCOURSE(); objCourse.course_name = "B.Tech"; objCourse.course_desc = "Bachelor Of Technology"; objCourse.modified_date = DateTime.Now;//Adds an entity in a pending insert state to this System.Data.Linq.Table<TEntity>and parameter is the entity which to be added OdContext.COURSEs.InsertOnSubmit(objCourse);// executes the appropriate commands to implement the changes to the database OdContext.SubmitChanges();}The Update Operation

privatevoid UpdateCourse(){OperationDataContext OdContext = newOperationDataContext();//Get Single course which need to updateCOURSE objCourse = OdContext.COURSEs.Single(course => course.course_name == "B.Tech");//Field which will be update objCourse.course_desc = "Bachelor of Technology";// executes the appropriate commands to implement the changes to the database OdContext.SubmitChanges(); }

The DELETE Operation

privatevoid DeleteCourse(){OperationDataContext OdContext = newOperationDataContext();//Get Single course which need to DeleteCOURSE objCourse = OdContext.COURSEs.Single(course => course.course_name == "B.Tech");//Puts an entity from this table into a pending delete state and parameter is the entity which to be deleted. OdContext.COURSEs.DeleteOnSubmit(objCourse);// executes the appropriate commands to implement the changes to the database OdContext.SubmitChanges();}

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.

Sandeep Singh Shekhawat is a Software Developer. He is
awarded for C# Corner MVP(2013,2014 and 2015) and CodeProject MVP (2015). His
three articles at CodeProject are prize wining in article competition. His more
than 10... Read more