Data Insertion using Linq


Create a table using SqlServer , named as “States”



Create a sample asp.net project named as “LinqSample” add a new DBML file named as “Linq” into it.

Open Server Explorer , choose DB , Drag & Drop the table into the designer (Inside DBML).

Open Default.aspx in the Designer Drop a asp:button control

DoubleClick button add the below code

protected void Button1_Click(object sender, EventArgs e)
{
LinqDataContext context = new LinqDataContext();
State obj = new State();
obj.StateId = 1;
obj.State1 = "NewYork";
context.States.InsertOnSubmit(obj);
context.SubmitChanges();
}

For Bulk Insertion use the below code

protected void Button1_Click(object sender, EventArgs e)
{
LinqDataContext context = new LinqDataContext();
List lst=new List();
lst.Add(new State() { StateId = 1, State1 = "California" });
lst.Add(new State() { StateId = 2, State1 = "NewJersey" });
lst.Add(new State() { StateId = 3, State1 = "NewYork" });
context.States.InsertAllOnSubmit(lst);
context.SubmitChanges();

}