使用linq访问数据库

上传人:wt****50 文档编号:45858484 上传时间:2018-06-19 格式:PDF 页数:40 大小:2.18MB
返回 下载 相关 举报
使用linq访问数据库_第1页
第1页 / 共40页
使用linq访问数据库_第2页
第2页 / 共40页
使用linq访问数据库_第3页
第3页 / 共40页
使用linq访问数据库_第4页
第4页 / 共40页
使用linq访问数据库_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《使用linq访问数据库》由会员分享,可在线阅读,更多相关《使用linq访问数据库(40页珍藏版)》请在金锄头文库上搜索。

1、使用使用LINQ访问数据库访问数据库王然王然 微软认证讲师 点击添加点击添加MSN机器人小新机器人小新点击添加点击添加MSN机器人小新机器人小新为您收听下载为您收听下载MSDN中文网络广播课程加油助力中文网络广播课程加油助力!本次课程内容包括本次课程内容包括本次课程内容包括本次课程内容包括概 LINQ to SQL 概览 手动创建映射关系手动创建映射关系 使用 LINQ to SQL 类 使用主/外键关系 数据分页 数据分页 修改数据 调用存储过程议程议程议程议程概概 LINQ to SQL 概概览览 手动创建映射关系手动创建映射关系 使用 LINQ to SQL 类 使用主/外键关系 数据分

2、页 数据分页 修改数据 调用存储过程LINQ to SQL 概览概览LINQ to SQL 概览概览把类数过系 把.NET 类和 SQL 数据通过关系进行映射 把 LINQ 查询转化为 SQL 语言进行执行把 LINQ 查询转化为 SQL 语言进行执行 支持对插入,更新,删除操作进行跟踪. 支持实体级别的验证规则 构建于 ADO NET之上并且集成连接池和事 构建于 ADO.NET之上并且集成连接池和事 务处理LINQ to SQL 功能功能LINQ to SQL 功能功能public partial class Product public partial class Product bli

3、 bli i ti t PdtIDPdtIDpupubliblic c i in nt t P Prorod ducuctIDtID; public string ProductName; public Nullable SupplierID; public string ProductName; public Nullable SupplierID; public Nullable CategoryID;public Nullable CategoryID;public Nullable CategoryID;public Nullable CategoryID; public class

4、NorthwindDataContext : DataContext public class NorthwindDataContext : DataContext bli T blPdt Pdtbli T blPdt Pdtpupubliblic c T Ta ablble P Prorod ducuct ts; s; h i ddbh i d()h i ddbh i d()NortNorth hw wi in nd dDataContext DataContext dbdb = new Nort = new North hw wi in nd dDataContextDataContext

5、()();DataList1.DataSource = from product in db.Products where ;DataList1.DataSource = from product in db.Products where p product.UnitsInStock 0roduct.UnitsInStock 0p p select product;DataList1.DataBind();select product;DataList1.DataBind();LINQ to SQL 架构架构from c in db.Customersfrom c in db.Customer

6、sfrom c in db.Customersfrom c in db.Customers h i “d“h i “d“h i “d“h i “d“ApplicationLINQ to SQL 架构架构w wh hereere c.Cc.Ci ityty = “ “LonLond donon“ “w wh hereere c.Cc.Ci ityty = “ “LonLond donon“ “ selectselect new c.Name, c.Phone new c.Name, c.Phone selectselect new c.Name, c.Phone new c.Name, c.Ph

7、one ApplicationLINQ QueryObjectsSubmitChanges ()LINQ for SQL (ADO.NET)Services:Services: - - Change trackingChange tracking - - Concurrency controlConcurrency controlServices:Services: - - Change trackingChange tracking - - Concurrency controlConcurrency control - - - - Object identityObject identit

8、yObject identityObject identitySQL QueryRowsSQL or Stored- - - - Object identityObject identityObject identityObject identityStored Procsselect Name, Phoneselect Name, Phoneselect Name, Phoneselect Name, Phone from customersfrom customersfrom customersfrom customersSQLServerfrom customersfrom custom

9、ersfrom customersfrom customers where city = Londonwhere city = Londonwhere city = Londonwhere city = London创建对象映射创建对象映射创建对象映射创建对象映射 为了给一个特定数据库创建一个对象模型, 我们必须将类映射到数据库实体当中。我须将类映射到数据库实体中 有三种方式可以创建对象映射: 手动创建对象映射为现有的对象添加属性 手动创建对象映射:为现有的对象添加属性 使用提供的设计器来自动生成对象映射 使用命令行的 SQLMetal 工具生成映射议程议程议程议程概 LINQ to SQL

10、概览 手动创建映射关系手动创建映射关系手动创建映射关系手动创建映射关系 使用 LINQ to SQL 类 使用主/外键关系 数据分页 数据分页 修改数据 调用存储过程手动创建对象映射手动创建对象映射手动创建对象映射手动创建对象映射令 添加 Using 指令 using System.Data.Linq;gyq; using System.Data.Linq.Mapping; 使用属性声明 使用属性声明 Table 属性 Column 属性添加属性声明添加属性声明添加属性声明添加属性声明T bl (N“C“)Table(Name = “Customers“) public class Custo

11、mer 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创建并使用创建并使用 LINQ to SQL创建并使用创建并使用 LINQ

12、to SQL 数据模型数据模型创建并使用创建并使用 LINQ to SQL 数据模型数据模型数据表映射数据表映射数据表映射数据表映射数实体类之系 映射数据表和实体类之间的关系 使用数据库中典型的主/外键进行表示使用数据库中典型的主/外键进行表示 支持灵活的关系查询并且不用写任务的 SQL 代码就可以执行处理过程SQL 代码就可以执行处理过程议程议程议程议程概 LINQ to SQL 概览 手动创建映射关系手动创建映射关系 使用 LINQ to SQL 类 使用主使用主/外键关系外键关系 数据分页 数据分页 修改数据 调用存储过程主主/外键关系外键关系Products TableSupplier

13、s Table主主/外键关系外键关系Categories TableForeign Key Relationships to the Suppliers andto the Suppliers and Categories TablesLINQ设计器中的关系映射设计器中的关系映射LINQ设计器中的关系映射设计器中的关系映射模型中的代码关联模型中的代码关联public partial class Product public int ProductID;public partial class Product public int ProductID;模型中的代码关联模型中的代码关联public

14、 string ProductName; public Nullable SupplierID; public Nullable CategoryID;public string ProductName; public Nullable SupplierID; public Nullable CategoryID;public Supplier Supplier; public Category Category;public Supplier Supplier; public Category Category; public partial class Supplier public in

15、t SupplierID;public partial class Supplier public int SupplierID; public string CompanyName; public EntitySet Products; public string CompanyName; public EntitySet Products; public partial class Category public int CategoryID;public partial class Category public int CategoryID; public EntitySet Products;public EntitySet Products;public EntitySet Products;public EntitySet Products; Creating and usingCreating and usingCreating and using Associations with the DLINQ Creating and using Associations with the DLINQ DesignerDesigner使用主使用主/外键关系

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号