Saturday, 27 April 2013

Basic Insert Operation to SQL Table Using LINQ

Hii ,

Here i am going to tell you something about Language Integrated Query in short LINQ.
For permofing DML operation (Insert, Update , Delete) to the data in your database, for that you need to write SQL query which modify the information of your database table . Here i am giving a basic example to how to use LINQ with your application using C#.

For inserting a value :

Step 1 :  Add an EDMX to your application.

Step 2 :  Select database from your SQL SERVER 

Step 3:  Add a .aspx page to your application

Step 4 : Add namespace of your entity model in your aspx.cs page 

Step 5 : Supose your table stucture is : 

ID - Int Idenntity Primary Key
Name - Nvarchar(20)
Age - int

and page have two textboxes with button.
Now you want to insert records uisng LINQ to your SQL table.

on button click event write the below code :

protected void btn_click(object sender, Eventargs e)
{
            EntityModel obj = new EntityModel ();   // obj is an object of EntityModel
            YourTable tbl = new  YourTable ();       // tbl is an object of your Table
             tbl.Name = textbox1.Text;
             tbl.Age = Convert.Toint32(textbox1.Text);
             obj.AddTotbl();                                  // Add Data to your table 
             obj.saveChanges();                             // Save the changes to your SQL Table
}



1 comment: