I Love LINQ!
Posted by: Matthew on Wednesday, April 28, 2010
I started using LINQ about 8 months ago, before that I was strictly a stored procedure kind-of-guy. LINQ has made my programming life so much easier, no longer do I have to sit there writing SP’s for every database query I require.
These days my database creation steps look like this:
1. Create database in SQL Server Management Studio
2. Open the Server Explorer in Visual Studio and connect to my database
3. Add a LINQ to SQL Classes file to my project
4. Drag the database tables from the Server Explorer into the LINQ to SQL file
5. Create any associations / inheritance between tables (Right click on the table object, hover over the Add menu option)
6. Save my project
Once these steps have been finished and after I press the save button Visual Studio automatically creates all the classes required for database interaction.
Accessing the database and tables via LINQ is also super easy. Lets say we have a program that sells hotdogs to people. Here’s how I would add a hotdog sale to the database:
hotdogDBContext db = new hotdogDBContext();
sale newSale = new sale();s.price = 2;
s.seller = "Frank"
s.date = DateTime.Now;db.sale.InsertOnSubmit(newSale);
db.submitChanges();
Now lets say we want to grab all of Franks sales from the database:
hotdogDBContext db = new hotdogDBContext();
IEnumerable<sale> franksSales = from s in db.sales
where s.seller == "Frank"
select s;IEnumerable<sale> franksSalesData = franksSales.ToList();
foreach(sale s in franksSalesData)
{
Response.Write(s.Seller + " " + s.price + " " s.date);
}
Pretty simple isn’t it.
LINQ is just another great way for programmers to save time and make coding a little more enjoyable.
Social
Latest Posts
- mixcraft 6
- install asp.net session state sql db
- optus galaxy tab apn settings
- the big nerd ranch guide, xcode 4.2 and mainwindow.xib
- skyrim, oh skyrim
- earth from space - time lapse
- call of duty - modern warfare 3
- major interface updates, and keeping clients happy
Recent Tweets
-
<----- TWEETS ----->
Categories
- acoustica mixcraft 5 (3)
- building a synth (0)
- fun things (24)
- galaxy tab (2)
- gaming (3)
- life (3)
- music production (2)
- programming (4)
- technology (2)
