博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用LINQ访问数据库

Posted on 2010-12-07 20:25  itcfj  阅读(374)  评论(0编辑  收藏  举报

? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

议程
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

LINQ to SQL 概览
? 把.NET 类和 SQL 数据通过关系进行映射 ? 把 LINQ 查询转化为 SQL 语言进行执行 ? 支持对插入,更新,删除操作进行跟踪. ? 支持实体级别的验证规则
? 构建于 ADO.NET之上并且集成连接池和事
    务处理

LINQ to SQL 功能
ontext  {
Northwin DataList
select product;
DataList1.DataBind();

LINQ to SQL 架构
from  c  in  db.Customers
Application
where  c.City  ==  "London" select
new  {  c.Name,  c.Phone  }
Objects
LINQ Query
SubmitChanges
()
Services:
LINQ for SQL
-  Change  tracking
(ADO.NET)
-  Concurrency  control
-  Object  identity
SQL or
SQL Query
Rows
Stored
    Procs
select  Name,  Phone from  customers
where  city  =  'London'
SQLServer

创建对象映射
? 为了给一个特定数据库创建一个对象模型,
    我们必须将类映射到数据库实体当中。 ? 有三种方式可以创建对象映射:
- 手动创建对象映射:为现有的对象添加属性 - 使用提供的设计器来自动生成对象映射 - 使用命令行的 SQLMetal 工具生成映射

议程
? LINQ to SQL 概览
? 手动创建映射关系
? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

手动创建对象映射
? 添加 Using 指令
- using System.Data.Linq;
- using System.Data.Linq.Mapping;
? 使用属性声明
- Table 属性
- Column 属性

添加属性声明
? [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; }
}

议程
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

创建并使用 LINQ to SQL 数据模型

数据表映射
? 映射数据表和实体类之间的关系 ? 使用数据库中典型的主/外键进行表示 ? 支持灵活的关系查询并且不用写任务的
    SQL 代码就可以执行处理过程

议程
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

主/外键关系
Products Table
Suppliers Table
Categories Table
Foreign Key Relationships to the Suppliers and Categories Tables

LINQ设计器中的关系映射

模型中的代码关联
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;
}

 

Creating and using
Associations with the DLINQ Designer

使用主/外键关系
    Supplier.CompanyName Product.ProductName

代码
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  (Product product in supplier.Products)  {
Response.Write("-- ");
Response.Write(product.ProductName); Response.Write("<br/>");
}
}

使用 Suppliers 和 Products 表
NorthwindDataContext db  = new NorthwindDataContext();
SupplierList.DataSource  = from s in db.Suppliers
    where s.Products.Count  >  2
select s;
SupplierList.DataBind();

使用数据关系进行绑定
<asp:Repeater 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>

查看结果

使用 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();

查看结果

议程
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系
? 数据分页
? 修改数据
? 调用存储过程

使用 Skip() 和 Take() 进行数据分页

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

议程
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

更新数据
NorthwindDataContext db  =  new NorthwindDataContext(); Product product  =  db.Products.Single(p  => p.ProductName== "Chai");
product.UnitsInStock  =  11;
product.ReorderLevel  =  10; product.UnitsOnOrder  =  2;
db.SubmitChanges();

插入数据
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();

删除数据
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();
}

议程
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

添加存储过程

调用存储过程

回顾
? LINQ to SQL 概览 ? 手动创建映射关系 ? 使用 LINQ to SQL 类 ? 使用主/外键关系 ? 数据分页
? 修改数据
? 调用存储过程

获取更多MSDN资源
?  MSDN中文网站
    http://msdn2.microsoft.com/zh‐cn ?  MSDN中文网络广播
    http://  www.microsoft.com/china/msdn/webcast ?  MSDN中文网络广播课程预告邮件
    http://www.microsoft.com/china/msdn/WebcastNewsletter/ ?  MSDN免费中文速递邮件 (MSDN  Flash)
    http://msdn2.microsoft.com/zh‐cn/flash ?  MSDN开发中心
http://msdn2.microsoft.com/zh‐cn/developercenters
?  MSDN图书中心
http://www.microsoft.com/china/msdn/book

Question & Answer
    如需提出问题,请在此区域输入文字,并单击 “问题和解答”右上方的“提问”按钮即可。
您也可以选择在微软中文技术论坛上寻求帮助,MSDN中文网络 广播的讲师们会定期在论坛上为大家解答与课程相关的技术问题。
http://forums.microsoft.com/china