LINQ TO SQL Lng Trn Hy Hin hyhiengmail




































- Slides: 36
LINQ TO SQL Lương Trần Hy Hiến - hyhien@gmail. com
Phương thức mở rộng
Biểu thức Lambda (input parameters) => {executioncode} “goes into”
Khởi tạo đối tượng Hang. Hoa hh = new Hang. Hoa(); Hang. Hoa hh = new Hang. Hoa(1, “Bánh đậu”); Hang. Hoa hh = new Hang. Hoa{ Ma. HH = 1, Ten. HH = “Bánh đậu” };
C# 3. 0 Language Extensions Query expressions var hanghoa = from c in dshanghoa where c. Ma. Loai == "WA" Local variable type select new { c. Ten. HH, c. Don. Gia }; inference Lambda expressions var hanghoa = dshanghoa. Where(c => c. Ma. Loai == "WA"). Select(c => new { c. Ten. HH, c. Don. Gia }); Extension methods Anonymous types Object initializers
Danh sách các từ khóa Restriction Where Projection Select Ordering Order. By, Order. By. Descending, Then. By. Decending Group. By Quantifiers Any, All, Contains Partitioning Take, Skip, Take. While, Skip. While Sets Distinct, Union, Concat, Intersect, Except Elements First, First. Or. Default, Last, Single, Element. At Aggregation Count, Sum, Min, Max, Average Conversion To. Array, To. List, To. Dictionary, To. Lookup
Query Comprehensions Project Select <expr> Filter Where <expr> Test Any(<expr>), All(<expr>) Join <expr> [ [ Left | Right ] Outer ] Join <expr> On <expr> Group By <expr> Aggregate Count(<expr>), Sum(<expr>), Min(<expr>), Max(<expr>), Avg(<expr>) Partition Top <expr> Set Distinct, Union, Intersect, Except Order By <expr>
Các loại Linq to Object Linq to SQL Linq to XML
Linq to SQL 9 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
LINQ to SQL Mapping Database Data. Context Table Class View Class Column Field / Property Relationship Field / Property Stored Procedure Method
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 <Ten. CSDL>. dbml). Ví dụ: Truong. Hoc. dbml Khởi tạo đối tượng CSDL đã ánh xạ (đối tượng <Ten. CSDL><Data. Context>). Ví dụ: Truong. Hoc. Data. Context Sử dụng đối tượng CSDL trên để thao tác CSDL - 11 -
Loại CSDL LINQ to SQL hỗ trợ Chỉ hỗ trợ các loại CSDL sau: Microsoft SQL Server Compact (*. sdf) Microsft SQL Server Database File (*. mdf) - 12 -
Thực hiện ánh xạ ORM Tạo file Ten. CSDL. 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. - 13 -
Accessing Data Today Sql. Connection c = new Sql. Connection(…); c. Open(); Sql. Command cmd = new Sql. Command( @"SELECT c. Ten. HH, c. Don. Gia FROM Hang. Hoa c WHERE c. Ma. Loai = @p 0"); cmd. Parameters. Add. With. Value("@p 0", 1); Data. Reader dr = c. Execute(cmd); while (dr. Read()) { string ten_hhh = dr. Get. String(0); string gia = dr. Get. String(1); } dr. Close(); Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks
Accessing With LINQ to SQL public class Hoa { … } public class QLBan. Hang: Data. Context { public Table<Hang. Hoa> Hang. Hoas; … } QLBan. Hang db = new QLBan. Hang(…); var dshh = from c in db. Hang. Hoas where c. Ma. Loai == 1 select new { c. Ten. HH, c. Don. Gia }; Tables are like collections Integrated query syntax Strongly typed results
INTRO DEMO
Application Architecture With LINQ to SQL from c in db. Hang. Hoas where c. Ma. Loai == 1 select c. Ten. HH LINQ Query Application Objects db. Hang. Hoas. Insert. On. Submit(c 1); c 2. Ma. Loai = 2; db. Hang. Hoas. Delete. On. Submit(c 3); Submit. Changes() LINQ to SQL Query SELECT Ten. HH FROM Hang. Hoa WHERE Ma. Loai = 1 Rows SQL Server DML or SProcs INSERT INTO Hang. Hoa… UPDATE Hang. Hoa … DELETE FROM Hang. Hoa …
Lưu ý var query = from c in db. Hang. Hoas where c. Ma. Loai == 1 select c. Ten. HH; var query = db. Hang. Hoas. Where(c => c. Ma. Loai == 1). Select(c => c. Ten. HH);
Lin. Q thực thi như thế nào? from c in ds. Hang. Hoa where c. Ma. Loai == “NO" select new { c. . Ten. HH, c. Don. Gia }; ds. Hang. Hoa. Where(c => c. Ma. Loai == “NO"). Select(c => new { c. Ten. HH, c. Don. Gia });
Examples
Examples
Examples
Where
Order. By
Group. . . by. . .
Group. . . by. . .
Join
Join
Aggregate Function Count Sum Min Max Average
Max
Count
Take
Take. While
Skip
Skip. While
Q&A 36