How to insert,update and delete the records in Database using LINQ concepts

By
Hi Friends ! Today , i am going to Explain "How to insert,update and delete the records in database using LINQ concepts".In this concepts, i will use Data context class to perform this whole operations(insert,delete,update). You can also easily display the records with the help of Data Context class.Linq concepts are very helpful for our .net applications. Every things which you can perform in SQL Query, perform in Linq query also.The Linq query is more simplest than SQL. Here i have explained this in graphical way which is more user friendly. Any user can easily insert,update and delete the records in database.Here each User can easily see the inserted ,updated and deleted the records on your computer screen. 
There are following steps to understand the whole concepts as given below:-
Step 1:- First open SQL Server Management studio-->Create a table (student12) by run the following commands .


create table student12 (sid int primary key,name varchar(20),age int)

Note :-
  • Your table should be a primary otherwise you can't insert,update and delete the records in database.
  • You can create a table at run time using Linq but here i have not created it.If you want, you can create it. 
Step 2:-  First open your visual studio -->File -->New -->website -->Select ASP.NET Empty website -->OK --> Right click on your project(website) -->Add New Items -->Select LINQ  to SQL Classes  as shown below --> Press Add Button.


data class

Step 3:- Now press Server Explorer --> Open your database Tables -->Drag and Drop table (student12) on Data Classes.dbml page as shown below:-

drag_table

Step 4:- Now open Solution Explorer --> Add a web form (Default.aspx) --> Drag and drop Label ,Text Box,List Box and Button controls on the page from toolbox as shown below:-


design

Note : -
Here each user can perform insert ,update and delete operations on web page with database.

Step 5:- Now Write the c# codes under each button (inset,update,delete) as given below:-
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext dc_obj = new DataClassesDataContext();
     var display_data = from st in dc_obj.student12s where st.sid >=0 select st;
        foreach(student12 st_da in display_data)
        {
            ListBox1.Items.Add(st_da.name);
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataClassesDataContext dc_obj = new DataClassesDataContext();
        student12 obj = new student12 {sid=int.Parse(TextBox1.Text),name=TextBox2.Text,age=int.Parse(TextBox3.Text)};
        dc_obj.student12s.InsertOnSubmit(obj);
        dc_obj.SubmitChanges();
        Label1.Text = "Data inserted successfully...";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DataClassesDataContext dc_obj = new DataClassesDataContext();
        student12 st_obj = dc_obj.student12s.Single(s => s.sid ==int.Parse(TextBox4.Text));
        st_obj.name = TextBox5.Text;
        dc_obj.SubmitChanges();
        Label1.Text = "Successfully Updated....";
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        DataClassesDataContext dc_obj = new DataClassesDataContext();
        student12 st_obj = dc_obj.student12s.Single(s => s.sid == int.Parse(TextBox6.Text));
        dc_obj.student12s.DeleteOnSubmit(st_obj);
        dc_obj.SubmitChanges();
        Label1.Text = "Successfully Deleted....";
    }
}

Step 6:- Now Run the Application (Press F5)-->Enter the required field details and press insert ,update and delete operations respectively-->You will see the following output as shown below:-


output

 Note:-

  • You can use this concepts in your any asp.net application.
  • You can Run this whole on your Visual studio without facing any problem.
I hope this is helpful to you.

Download Attached file
   Download

0 comments:

Post a Comment

Powered by Blogger.