Linq-to-SQL-Insert-syntex

Linq-to-SQL-Insert-syntex


Referring to http://www.revenmerchantservices.com/page/Linq-to-Sql.aspx

Insert new Item into DB using Linq to SQl

First we will add Locations in to .DBML file. Drag and drop the Locations table into .DBML file

and perform below action to insert new location and show it on grid.

AdventureWorksDataContext dbc = new AdventureWorksDataContext();    // Create an object of Database context

Location locationinfo = new Location();   // Create entity of Location

locationinfo.Name = "Pune";    // Populate entity with Names,and other field

locationinfo.Availability = 1.5M;

locationinfo.CostRate = 1.6M;

locationinfo.ModifiedDate = DateTime.Now;

dbc.Locations.InsertOnSubmit(locationinfo);    // Pass this location entity to Database context for insertation

dbc.SubmitChanges();     // update changes into SQL

var query = from locationEntity in dbc.Locations     // Query the locations table for newly inserted field locations

where locationEntity.Name == "Pune"

select locationEntity;

GridView1.DataSource = query;     // Bind the query to Grid

GridView1.DataBind();

- Satalaj

 

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5