1. LINQ to SQL 概覽
• 把.NET 類和 SQL 數據通過關系進行映射
• 把 LINQ 查詢轉化為 SQL 語言進行執行
• 支持對插入,更新,刪除操作進行跟蹤.
支持實體級別的驗證規則
• 構建於 ADO.NET之上並且集成連接池和事務處理
1.1. LINQ to SQL 功能
1.2. LINQ to SQL 架構
1.3. 創建對象映射
• 為了給一個特定數據庫創建一個對象模型,我們必須將類映射到數據庫實體當中。
• 有三種方式可以創建對象映射:
–手動創建對象映射:為現有的對象添加屬性
– 使用提供的設計器來自動生成對象映射
– 使用命令行的SQLMetal 工具生成映射
2. 手動創建映射關系
• 添加 Using 指令
– using System.Data.Linq;
– using System.Data.Linq.Mapping;
• 使用屬性聲明
– Table 屬性
– Column 屬性
2.1. 添加屬性聲明
• [Table(Name = "Customers")]
• public class Customer
• {
• [Column]
• public string CustomerID { get; set; }
• [Column]
• public string City { get; set; }
• public override string ToString()
• {
• return CustomerID + "\t" + City;
• }
• }
3. 使用LINQ to SQL 類
3.1. 數據表映射
• 映射數據表和實體類之間的關系
• 使用數據庫中典型的主/外鍵進行表示
• 支持靈活的關系查詢並且不用寫任務的SQL 代碼就可以執行處理過程
4. 使用主/外鍵關系
4.1. 主/外鍵關系
4.2. LINQ設計器中的關系映射
4.3. 模型中的代碼關聯
public partial class Product { public int ProductID; public string ProductName; public Nullable<int> SupplierID; public Nullable<int> CategoryID; public Supplier Supplier; public Category Category; } public partial class Supplier { public int SupplierID; public string CompanyName; public EntitySet<Product> Products; } public partial class Category { public int CategoryID; public EntitySet<Product> Products; }
4.4. 使用主/外鍵關系
4.5. 代碼
NorthwindDataContext db = new NorthwindDataContext(); (); var suppliers = from s in db.Suppliers where s.Products.Count > 2 select s; foreach (Supplier supplier in suppliers) { Response.Write("<h3>" + supplier.CompanyName + "</h3>"); foreach (Pro duct produc t in supplier.Products) { Response.Write("-- "); Response.Write(product.ProductName); Response.Write("<br/>"); } }
4.6. 使用 Suppliers 和 Products 表
NorthwindDataContext db = new NorthwindDataContext(); SupplierList.DataSource = from s in db.Suppliers where s.Products.Count > 2 select s; SupplierList.DataBind();
4.7. 使用數據關系進行綁定
<asp:Repeater ID "SupplierList" runat "server"> ID= SupplierList runat= server > <ItemTemplate> <h3> <%# Eval("CompanyName") %> </h3> <asp:Repeater DataSource='<%# Eval("Products")%>' runat="server"> <ItemTemplate> -- <%# Eval("ProductName") %> <br /> </ItemTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater>
4.8. 查看結果
4.9. 使用 join 連結數據表
NorthwindDataContext db = new NorthwindDataContext(); var results = from c in db.Customers join o in db.Orders on c.CustomerID equals o.CustomerID into custOrders from o in custOrders select new { Customer = c.CompanyName, OrderDate = o.OrderDate, OrderTotal = o.OrderDetails.Sum(d=>d.UnitPrice) }; GridView1.DataSource = results; GridView1.DataBind();
4.10. 查看結果
5. 數據分頁
5.1. 使用 Skip() 和 Take() 進行數據分頁
5.2. Skip() 和 Take() 方法
int startRow = Convert.ToInt32(Request.QueryString["startRow"]); NorthwindDataContext db = new NorthwindDataContext(); var results = from c in db.Customers join o in db.Orders on c.CustomerID equals o.CustomerID into custOrders from o in custOrders select new { Customer = c.CompanyName, OrderDate = o.OrderDate, OrderTotal = o.OrderDetails.Sum(d=>d.UnitPrice) }; GridView1.DataSource = results.Skip(startRow).Take(10); GridView1.DataBind();
6. 修改數據
6.1. 更新數據
NorthwindDataContext db = new NorthwindDataContext(); Product product = db.Products.Single(p => p.ProductName== "Chai"); product.UnitsInStock = 11; product.ReorderLevel = 10; product.UnitsOnOrder = 2; db.SubmitChanges();
6.2. 插入數據
NorthwindDataContext db = new NorthwindDataContext(); Supplier supplier = new Supplier(); supplier.CompanyName = "Scott Guthrie"; supplier.HomePage = "http://weblogs.asp.net/scottgu"; Product product1 = new Product(); product1.ProductName = "LINQ Talk"; product1.UnitPrice = new Decimal(99.9); Product product2 = new Product(); product2.ProductName = "ASP.NET Tips/Tricks Talk"; product2.UnitPrice = new Decimal(101.99); supplier.Products.Add(product1); supplier.Products.Add(product2); db.Suppliers.InsertOnSubmit(supplier); db.SubmitChanges();
6.3. 刪除數據
NorthwindDataContext db = new NorthwindDataContext(); var supplier = db.Suppliers.FirstOrDefault(s=>s.CompanyName == “ABC"); if ((supplier != null) && (supplier.Products.Count == 0)) { db.Suppliers.DeleteOnSubmit(supplier); db.SubmitChanges(); }