Công nghệ thông tin - Linq to sql

HangHoa hh = new HangHoa();

HangHoa hh = new HangHoa(1, “Bánh đậu”);

HangHoa hh = new HangHoa{

MaHH = 1, TenHH = “Bánh đậu”

pdf36 trang | Chia sẻ: Mr Hưng | Lượt xem: 837 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Công nghệ thông tin - Linq to sql, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
LINQ TO SQL Lương Trần Hy Hiến - hyhien@gmail.com Phương thức mở rộng (input parameters) => {executioncode} “goes into” Biểu thức Lambda 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” }; C# 3.0 Language Extensions var hanghoa = from c in dshanghoa where c.MaLoai == "WA" select new { c.TenHH, c.DonGia }; var hanghoa = dshanghoa .Where(c => c.MaLoai == "WA") .Select(c => new { c.TenHH, c.DonGia }); 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 Query Comprehensions Project Select Filter Where Test Any(), All() Join [ [ Left | Right ] Outer ] Join On Group Group By Aggregate Count(), Sum(), Min(), Max(), Avg() Partition Top Set Distinct, Union, Intersect, Except Order Order By Các loại  Linq to Object  Linq to SQL  Linq to XML 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 9 LINQ to SQL Mapping Database Table View Column Relationship Stored Procedure DataContext Class Class Field / Property Field / Property 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 .dbml). Ví dụ: TruongHoc.dbml  Khởi tạo đối tượng CSDL đã ánh xạ (đối tượng ). Ví dụ: TruongHocDataContext  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  Microsoft SQL Server Compact (*.sdf)  Microsft SQL Server Database File (*.mdf) - 12 - 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. - 13 - Accessing Data Today SqlConnection c = new SqlConnection(); c.Open(); SqlCommand cmd = new SqlCommand( @"SELECT c.TenHH, c.DonGia FROM HangHoa c WHERE c.MaLoai = @p0"); cmd.Parameters.AddWithValue("@p0", 1); DataReader dr = c.Execute(cmd); while (dr.Read()) { string ten_hhh = dr.GetString(0); string gia = dr.GetString(1); } dr.Close(); Accessing With LINQ to SQL public class Hoa { } public class QLBanHang: DataContext { public Table HangHoas; } QLBanHang db = new QLBanHang(); var dshh = from c in db.HangHoas where c.MaLoai == 1 select new { c.TenHH, c.DonGia }; INTRO DEMO Application Architecture With LINQ to SQL Application LINQ to SQL from c in db.HangHoas where c.MaLoai == 1 select c.TenHH LINQ Query SQL Query SELECT TenHH FROM HangHoa WHERE MaLoai = 1 Rows Objects SubmitChanges() DML or SProcs db.HangHoas.InsertOnSubmit(c1); c2.MaLoai = 2; db.HangHoas.DeleteOnSubmit(c3); INSERT INTO HangHoa UPDATE HangHoa DELETE FROM HangHoa 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); 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 }); Examples Examples Examples Where OrderBy Group ... by ... Group ... by ... Join Join Aggregate Function  Count  Sum  Min  Max  Average Max Count Take TakeWhile Skip SkipWhile Q & A 36

Các file đính kèm theo tài liệu này:

  • pdfasp_net_linq2sql_1__3596.pdf
Tài liệu liên quan