In this article, i will explain how to select, insert, update, and delete a record from SQL database using LINQ. Using LINQ,
we can easily perform these basic DML (Data Manipulation Language) operations without writing
any sql queries.

We need to add an new item "LINQ to SQL Classes" for implementing the LINQ conecpts. Right click your solution click add an new item. On the open dialog choose the "LINQ to SQL Classes" item and click open button. After clicked the open button, it immediately asks the alert window for adding the dbml file in "App_Code". Click the ok button.

Open the ".dbml" file, and choose the "View->Server explorer" option. This server explorer shows the list of available connections. If your database connection is already available use that otherwise right click the connection and add an new connection with your SQL server. After made a connection select your table and drag into your .dbml file.

Initialize an object to this database like this,MyDatabaseDataContext objDatabase = new MyDatabaseDataContext();

Here MyDatabase is a file name of the LINQ class "MyDatabase.dbml". The linq class name always creates DataContext with the name of your file name. After initialize this object you can select, insert, delete, or update with this object and your table object.

Select

If we want to select the records from a table. First initialize an object to that table. Refer the following codes,

//Assign the new values to that tableobjUserDetails.Username = txtUsername.Text.Trim();objUserDetails.Password = txtpassword.Text.Trim();objUserDetails.emailid = txtEmailid.Text.Trim();

//update the changes to dbobjDatabase.SubmitChanges();

Delete

Delete also same as update method. While updating a records just called the "submitchanges" method only. But while deleting a record we also need to call the "DeleteOnSubmit" method before call the "submitchanges" method as like "InsertonSubmit".