使用LINQ访问数据库

 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();
}

 

转载于:https://www.cnblogs.com/lxh168/p/7476338.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值