Bản thuyết trình đang được tải. Xin vui lòng chờ

Bản thuyết trình đang được tải. Xin vui lòng chờ

Lương Trần Hy Hiến - hyhien@gmail.com LINQ to SQL Lương Trần Hy Hiến - hyhien@gmail.com.

Các bản thuyết trình tương tự


Bản thuyết trình với chủ đề: "Lương Trần Hy Hiến - hyhien@gmail.com LINQ to SQL Lương Trần Hy Hiến - hyhien@gmail.com."— Bản ghi của bản thuyết trình:

1 Lương Trần Hy Hiến - hyhien@gmail.com
LINQ to SQL Lương Trần Hy Hiến -

2 Phương thức mở rộng

3 (input parameters) => {executioncode}
Biểu thức Lambda (input parameters) => {executioncode} “goes into”

4 Khởi tạo đối tượng HangHoa hh = new HangHoa{
HangHoa hh = new HangHoa(1, “Bánh đậu”); HangHoa hh = new HangHoa{ MaHH = 1, TenHH = “Bánh đậu” };

5 C# 3.0 Language Extensions
7/25/2019 9:57 PM C# 3.0 Language Extensions Query expressions var hanghoa = from c in dshanghoa where c.MaLoai == "WA" select new { c.TenHH, c.DonGia }; Local variable type inference Lambda expressions var hanghoa = dshanghoa .Where(c => c.MaLoai == "WA") .Select(c => new { c.TenHH, c.DonGia }); Extension methods Object initializers Anonymous types © 2006 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.

6 Danh sách các từ khóa Restriction Where Projection Select Ordering
OrderBy, OrderByDescending, ThenBy, ThenByDecending Grouping GroupBy Quantifiers Any, All, Contains Partitioning Take, Skip, TakeWhile, SkipWhile Sets Distinct, Union, Concat, Intersect, Except Elements First, FirstOrDefault, Last, Single, ElementAt Aggregation Count, Sum, Min, Max, Average Conversion ToArray, ToList, ToDictionary, ToLookup

7 Query Comprehensions Project Select <expr> Filter
Where <expr> Test Any(<expr>), All(<expr>) Join <expr> [ [ Left | Right ] Outer ] Join <expr> On <expr> Group Group By <expr> Aggregate Count(<expr>), Sum(<expr>), Min(<expr>), Max(<expr>), Avg(<expr>) Partition Top <expr> Set Distinct, Union, Intersect, Except Order Order By <expr>

8 Các loại Linq to Object Linq to SQL Linq to XML

9 Linq to SQL Là kỹ thuật ORM (Object Relation Mapping) của Microsoft dùng để ánh xạ quan hệ CSDL sang quan hệ đối tượng. Giúp cho việc lập trình giao tiếp CSDL dễ dàng hơn. Hỗ trợ những thao tác cơ sở dữ liệu: thêm, xóa, sửa, cập nhật. Hỗ trợ transaction, view, stored procedure

10 LINQ to SQL Mapping Database DataContext Table Class View Class Column
Field / Property Relationship Field / Property Stored Procedure Method

11 Các bước thao tác Kết nối đến loại CSDL mà LINQ to SQL hỗ trợ
Thực hiện ánh xạ ORM (tạo file <TenCSDL>.dbml). Ví dụ: TruongHoc.dbml Khởi tạo đối tượng CSDL đã ánh xạ (đối tượng <TenCSDL><DataContext>). Ví dụ: TruongHocDataContext Sử dụng đối tượng CSDL trên để thao tác CSDL

12 Loại CSDL LINQ to SQL hỗ trợ
Chỉ hỗ trợ các loại CSDL sau: Microsoft SQL Server Microsoft SQL Server Compact (*.sdf) Microsft SQL Server Database File (*.mdf)

13 Thực hiện ánh xạ ORM Tạo file TenCSDL.dbml
Kéo tất cả các bảng vào cửa sổ Design Có thể kéo các View, Stored Procedure, Function vào cửa sổ Design Lưu ý khi đã thực hiện ánh xạ trong project sẽ xuất hiện file Web.config. Trong file này chứa chuỗi connectionstring. Khi đem ứng dụng qua máy khác chạy ta chỉ cần chỉnh sửa giá trị chuỗi connectionstring trong file này.

14 Accessing Data Today SqlConnection c = new SqlConnection(…); c.Open();
SqlCommand cmd = new SqlCommand( @"SELECT c.TenHH, c.DonGia FROM HangHoa c WHERE c.MaLoai 1); DataReader dr = c.Execute(cmd); while (dr.Read()) { string ten_hhh = dr.GetString(0); string gia = dr.GetString(1); } dr.Close(); Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks

15 Accessing With LINQ to SQL
public class Hoa { … } public class QLBanHang: DataContext { public Table<HangHoa> HangHoas; } Tables are like collections QLBanHang db = new QLBanHang(…); var dshh = from c in db.HangHoas where c.MaLoai == 1 select new { c.TenHH, c.DonGia }; Integrated query syntax Strongly typed results

16 INTRO DEMO

17 Application Architecture With LINQ to SQL
from c in db.HangHoas where c.MaLoai == 1 select c.TenHH db.HangHoas.InsertOnSubmit(c1); c2.MaLoai = 2; db.HangHoas.DeleteOnSubmit(c3); LINQ Query Objects SubmitChanges() LINQ to SQL SQL Query Rows DML or SProcs SELECT TenHH FROM HangHoa WHERE MaLoai = 1 INSERT INTO HangHoa… UPDATE HangHoa … DELETE FROM HangHoa … SQL Server

18 Lưu ý var query = from c in db.HangHoas where c.MaLoai == 1 select c.TenHH; var query = db.HangHoas.Where(c => c.MaLoai == 1).Select(c => c.TenHH);

19 LinQ thực thi như thế nào?
from c in dsHangHoa where c.MaLoai == “NO" select new { c..TenHH, c.DonGia }; dsHangHoa .Where(c => c.MaLoai == “NO") .Select(c => new { c.TenHH, c.DonGia });

20 Examples

21 Examples

22 Examples

23 Where

24 OrderBy

25 Group ... by ...

26 Group ... by ...

27 Join

28 Join

29 Aggregate Function Count Sum Min Max Average

30 Max

31 Count

32 Take

33 TakeWhile

34 Skip

35 SkipWhile

36 Q & A


Tải xuống ppt "Lương Trần Hy Hiến - hyhien@gmail.com LINQ to SQL Lương Trần Hy Hiến - hyhien@gmail.com."

Các bản thuyết trình tương tự


Quảng cáo bởi Google