ASp.Net day

Micro blog

Linq to SQL Insert syntex

May 22
by Satalaj 22. May 2009 09:37


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

 

learn Linq step by step

May 21
by Satalaj 21. May 2009 12:43


→ ♣ Videos are available here   http://www.asp.net/learn/linq-videos/

All 7 videos are step by step


Linq is language integrated query.
Below is simple examle of Linq syntex

We will query an array of string.

//Create an array of Strings

string[] arr = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".Split(",".ToCharArray());

//Populate listbox with array of strings

ListBox1.DataSource = arr;

//Bind array of strings to Listbox

ListBox1.DataBind();

//Using linq syntex we will query an array of strings and find the word "S"  and "M"

var
query = from alphabet in arr

                    where (alphabet == "S" || alphabet == "M")

                    select alphabet  ; 
                   // A query body must end with a select clause or a group clause 

foreach(var alpha in query)

{

Response.Write(alpha+"<Br/>");   //Write the word found

}


 


How simple it is ! ∞

About Satalaj

My name is Satalaj. I'm 2010 asp.net MVP. I write technical stuff here. www.satalaj.com

Ads by Lake Quincy Media

The best inline translator

Live lookup to see what asp.net developers are searching