Dev-In-Town:Linq To Sql by Chan Ming Man

1,796 views

Published on

Dev-In-Town

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,796
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
51
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Dev-In-Town:Linq To Sql by Chan Ming Man

  1. 1. devInTown<br />Chan Ming Man<br />C# MVP<br />http://chanmingman.spaces.live.com<br />Level 200<br />
  2. 2. devInTown<br />CRUD Using LINQ (Language Integrated Query)<br />Chan Ming Man<br />C# MVP<br />http://chanmingman.spaces.live.com<br />Level 200<br />
  3. 3. Agenda<br />The object model challenges the relational model<br />What is LINQ?<br />LINQ to SQL<br />
  4. 4. Helpful Experience<br />Visual Studio 2008 C# Express<br />SQL Server 2005 <br />ADO.NET<br />
  5. 5. The object model challenges the relational model<br />The relational model has been fantastically successful in a wide variety of application areas. However, it is not problem free. The problems have been made more visible by the rise in popularity of object-oriented programming languages such as C++, Java, and C#.<br />
  6. 6. Data…its our job<br />Querying and manipulating data has always been a fundamental part of our jobs as programmers<br />Data formats change, but core needs are the same<br />
  7. 7. Data Access(DBASE circa 1980s)<br />USE empl <br /> REPLACE ALL salary WITH (salary * 1.1) FOR supervises &gt; 0 <br /> LIST ALL fname, lname, salary FOR Supervises &gt; 0<br />Data querying and manipulation a core part of the programming model experience<br />Certainly had limitations, but it sure was useful<br />
  8. 8. But Challenges Still Remain…<br />How to retrieve non-relational data?<br />XML, RSS, Web Services, REST, AD, Files, etc.<br />How to interact with plain old objects?<br />How do you interact and query custom domain models?<br />How to enable rich data shaping & transformations?<br />Support flexible query composition (that is fast!)<br />How to enable clean code in both a strongly typed and dynamic language world?<br />
  9. 9. LINQ<br />Query, Set, and Transform Operations for .NET<br />Makes querying data a core programming concept <br />Works with all types and shapes of data<br />Relational databases<br />XML<br />Plain old Objects<br />Works with all .NET languages<br />New VB and C# have integrated language support<br />
  10. 10. Queries integrated with code (C#/VB)<br />What is LINQ?<br />Strongly-typed<br />Benefits from IntelliSense<br />Collections of objects<br />Relational data<br />XML<br />Unified Querying<br />Language INtegrated Queries<br />
  11. 11. LINQ Architecture<br />C# 3.0<br />VB 9.0<br />Others…<br />.NET Language-Integrated Query (LINQ)<br />LINQ Enabled Data Sources<br />LINQ enabled ADO.NET<br />LINQ <br />to <br />Entities<br />LINQ <br />To<br />SQL<br />LINQ<br /> to<br />XML<br />LINQ <br />To<br />Dataset<br />LINQ <br />To<br />Objects<br />XML<br />Objects<br />Relational Data<br />11<br />©2008 Pavel Yosifovich<br />
  12. 12. Where is LINQ to SQL in MVC?<br />Model<br />LINQ <br />To<br />SQL<br />Controller<br />View<br />
  13. 13. LINQ to SQL<br />Application<br />from c in db.Customers<br />where c.City == &quot;London&quot;<br />select c.CompanyName<br />db.Customers.Add(c1);<br />c2.City = “Seattle&quot;;<br />db.Customers.Remove(c3);<br />LINQ Query<br />Objects<br />SubmitChanges()<br />LINQ to SQL<br />SQL Query<br />Rows<br />DML or Stored Procedures<br />SELECT CompanyName<br />FROM Cust<br />WHERE City = &apos;London&apos;<br />INSERT INTO Customers…<br />UPDATE Customers …DELETE FROM Customers …<br />SQL Server<br />
  14. 14. LINQ to SQL fully support CRUD<br />C = Create = Insert<br />R = Read = Select<br />U = Update<br />D = Delete<br />
  15. 15. Create<br />LINQ<br />varnewCustomer = new Customer {<br />CustomerID = &quot;DLEAP&quot;,<br />CompanyName = &quot;DevLeap&quot;,<br />Country = &quot;Italy&quot; };<br />db.Customers.InsertOnSubmit(newCustomer);<br />SQL<br />INSERT INTO [Customers](CustomerID, CompanyName, ...)<br />VALUES(&quot;DLEAP&quot;, &quot;DevLeap&quot;, ...)<br />
  16. 16. Read<br />LINQ<br />var query =<br />from c in Customers<br />where c.Country == &quot;USA&quot;<br />&& c.State == &quot;WA&quot;<br />select new {c.CustomerID, c.CompanyName, c.City };<br />SQL<br />SELECT CustomerID, CompanyName, City<br />FROM Customers<br />WHERE Country = &apos;USA&apos;<br />AND Region = &apos;WA&apos;<br />
  17. 17. Update<br />LINQ<br />Customer cust = db.Customers.Single(p =&gt; p.CustomerID == 11111);<br />cust.LastName = “Thomas &quot;;<br />db.SubmitChanges();<br />SQL<br />UPDATE Customer LastName = “Thomas &quot;<br />SET WHERE CustomerID == 11111<br />
  18. 18. Delete<br />LINQ<br />varoldDetail = db.Order_Details.Single(<br />od =&gt; od.OrderID == 10422<br />&& od.ProductID == 26);<br />db.Order_Details.DeleteOnSubmit(oldDetail);<br />SQL<br />DELETE FROM [dbo].[Order Details]<br />WHERE [OrderID] = 10422 AND [ProductID] = 26 AND ...<br />
  19. 19. Demo …<br />
  20. 20. Resources - LINQ<br />MSDN Developer Center<br />http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx<br />LINQ Forum<br />http://forums.microsoft.com/msdn/showforum.aspx?forumid=123<br />Channel9 Linq Videos<br />http://channel9.msdn.com/tags/linq<br />Blogs<br />Scott Guthrie <br />http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx<br />Charlie Calvert <br />http://blogs.msdn.com/charlie/archive/2006/10/05/Links-to-LINQ.aspx<br />LINQ HOL<br />C# 3.0 Enhancements & LINQ (English)<br />
  21. 21. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.<br />The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.<br />

×