dev-in-town:linq to sql by chan ming man

21
1 devInTown 1 devInTown Chan Ming Man C# MVP http://chanmingman.spaces.live.com Level 200

Upload: quek-lilian

Post on 10-May-2015

1.504 views

Category:

Technology


0 download

DESCRIPTION

Dev-In-Town

TRANSCRIPT

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

1devInTown 1

devInTown

Chan Ming ManC# MVPhttp://chanmingman.spaces.live.comLevel 200

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

2devInTown 2

devInTown

Chan Ming ManC# MVPhttp://chanmingman.spaces.live.comLevel 200

CRUD Using LINQ (Language Integrated Query)

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

3devInTowndevInTown

Agenda

The object model challenges the relational model

What is LINQ?

LINQ to SQL

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

4devInTown

Helpful Experience

Visual Studio 2008 C# Express

SQL Server 2005

ADO.NET

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

5devInTowndevInTown

The object model challenges the relational model

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#.

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

6devInTowndevInTown

Data…its our job

Querying and manipulating data has always been a fundamental part of our jobs as programmers

Data formats change, but core needs are the same

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

7devInTowndevInTown

Data Access(DBASE circa 1980s) USE empl

REPLACE ALL salary WITH (salary * 1.1) FOR supervises > 0

LIST ALL fname, lname, salary FOR Supervises > 0

Data querying and manipulation a core part of the programming model experience

Certainly had limitations, but it sure was useful

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

8devInTowndevInTown

But Challenges Still Remain…How to retrieve non-relational data?

XML, RSS, Web Services, REST, AD, Files, etc.

How to interact with plain old objects?How do you interact and query custom domain models?

How to enable rich data shaping & transformations?Support flexible query composition (that is fast!)

How to enable clean code in both a strongly typed and dynamic language world?

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

9devInTown

LINQ

Query, Set, and Transform Operations for .NET

Makes querying data a core programming concept

Works with all types and shapes of dataRelational databasesXMLPlain old Objects

Works with all .NET languagesNew VB and C# have integrated language support

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

10devInTown

What is LINQ?

Strongly-typedBenefits from IntelliSense

Language INtegrated Queries

Collections of objectsRelational dataXML

Queries integrated with code (C#/VB)

Unified Querying

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

11devInTown

LINQ Enabled Data Sources

LINQ enabled ADO.NET

LINQ Architecture

LINQ to

Entities

LINQ To

SQL

LINQ to

XML

C# 3.0 VB 9.0 Others…

LINQ To

Dataset

LINQ To

Objects

XMLObjects Relational Data

.NET Language-Integrated Query (LINQ)

11©2008 Pavel Yosifovich

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

12devInTown

Where is LINQ to SQL in MVC?

Model

ControllerView

LINQ To

SQL

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

13devInTowndevInTown

LINQ to SQL

Application

SQL Server

LINQ to SQL

from c in db.Customerswhere c.City == "London"select c.CompanyName

LINQ Query

SQL Query

SELECT CompanyNameFROM CustWHERE City = 'London'

Rows

Objects SubmitChanges()

DML or Stored Procedures

db.Customers.Add(c1);c2.City = “Seattle";db.Customers.Remove(c3);

INSERT INTO Customers…UPDATE Customers …DELETE FROM Customers …

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

14devInTowndevInTown

LINQ to SQL fully support CRUD

C = Create = InsertR = Read = SelectU = UpdateD = Delete

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

15devInTowndevInTown

CreateLINQ

var newCustomer = new Customer {CustomerID = "DLEAP",CompanyName = "DevLeap",Country = "Italy" };db.Customers.InsertOnSubmit(newCustomer);

INSERT INTO [Customers](CustomerID, CompanyName, ...)VALUES("DLEAP", "DevLeap", ...)

SQL

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

16devInTowndevInTown

Read

var query =from c in Customerswhere c.Country == "USA"&& c.State == "WA"select new {c.CustomerID, c.CompanyName, c.City };

SELECT CustomerID, CompanyName, CityFROM CustomersWHERE Country = 'USA'AND Region = 'WA'

LINQ

SQL

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

17devInTowndevInTown

Update

Customer cust = db.Customers.Single(p => p.CustomerID == 11111);cust.LastName = “Thomas ";db.SubmitChanges();

UPDATE Customer LastName = “Thomas "SET WHERE CustomerID == 11111

SQL

LINQ

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

18devInTowndevInTown

Delete

var oldDetail = db.Order_Details.Single(od => od.OrderID == 10422&& od.ProductID == 26);db.Order_Details.DeleteOnSubmit(oldDetail);

DELETE FROM [dbo].[Order Details]WHERE [OrderID] = 10422 AND [ProductID] = 26 AND ...

SQL

LINQ

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

19devInTown 19

Demo …

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

20devInTown

Resources - LINQ

MSDN Developer Centerhttp://msdn2.microsoft.com/en-us/netframework/aa904594.aspx

LINQ Forumhttp://forums.microsoft.com/msdn/showforum.aspx?forumid=123

Channel9 Linq Videoshttp://channel9.msdn.com/tags/linq

BlogsScott Guthrie

http://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspx

Charlie Calvert http://blogs.msdn.com/charlie/archive/2006/10/05/Links-to-LINQ.aspx

LINQ HOLC# 3.0 Enhancements & LINQ (English)

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

21devInTown

© 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.

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.