




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
FluentData入门(一)-核心概念DbContext类这是FluentData的核心类,可以通过配置ConnectionString来定义这个类,如何连接数据库和对具体DbContext类这是FluentData的核心类,可以通过配置ConnectionString来定义这个类,如何连接数据库和对具体的哪个数据库进行数据查询操作。 DbCommand类这个类负责在相对应的数据库执行具体的每一个数据操作。 Events DbContext类定义了以下这些事件:OnConnectionClosedOnConnectionOpenedOnConnectionOpeningOnErrorOnExecutedOnExecuting可以在事件中,记录每个SQL查询错误或者SQL查询执行的 时间等信息。 Builders Builder用来创建Insert, Update, Delete等相关的DbCommand实例。 MappingFluentData可以将SQL查询结果自动映射成一个POCO(POCO - Plain Old CLR Object)实体类,也可以转换成一个dynamic类型。自动转成实体类:如果字段名中不包含下划线(_),将映射到具有相同名字的属性上,例如:字段名 Name 将映射到属性名 Name。如果字段名中不包含下划线(_),将映射到内嵌的属性上,例如:字段名 CategoryName 将映射到属性名 Category.Name。如果数据库字段名和属性名不一致,可以使用SQL的as让他们一致。自动转换成dynamic类型:对应dynamic类型,会为每个字段生成一个同名的属性,例如:字段名 Name 将映射到属性名 Name。什么时候应该主动释放资源? 如果使用UseTransaction或者UseSharedConnection,那么DbContext需要主动释放。如果使用UseMultiResult或者MultiResultSql,那么DbCommand需要主动释放。如果使用UseMultiResult,那么StoredProcedureBuilder需要主动释放。其他所有的类都会自动释放,也就是说,一个数据库连接只在查询开始前才进行,查询结束后会马上关闭。的哪个数据库进行数据查询操作。 DbCommand类这个类负责在相对应的数据库执行具体的每一个数据操作。 Events DbContext类定义了以下这些事件: OnConnectionClosed OnConnectionOpened OnConnectionOpening OnError OnExecuted OnExecuting可以在事件中,记录每个SQL查询错误或者SQL查询执行的 时间等信息。 Builders Builder用来创建Insert, Update, Delete等相关的DbCommand实例。 MappingFluentData可以将SQL查询结果自动映射成一个POCO(POCO - Plain Old CLR Object)实体类,也可以转换成一个dynamic类型。自动转成实体类:1 如果字段名中不包含下划线(_),将映射到具有相同名字的属性上,例如:字段名 Name 将映射到属性名 Name。2 如果字段名中不包含下划线(_),将映射到内嵌的属性上,例如:字段名 CategoryName 将映射到属性名 Category.Name。如果数据库字段名和属性名不一致,可以使用SQL的as让他们一致。自动转换成dynamic类型:3 对应dynamic类型,会为每个字段生成一个同名的属性,例如:字段名 Name 将映射到属性名 Name。 什么时候应该主动释放资源? 如果使用UseTransaction或者UseSharedConnection,那么DbContext需要主动释放。 如果使用UseMultiResult或者MultiResultSql,那么DbCommand需要主动释放。 如果使用UseMultiResult,那么StoredProcedureBuilder需要主动释放。其他所有的类都会自动释放,也就是说,一个数据库连接只在查询开始前才进行,查询结束后会马上关闭。FluentData入门(二)-创建DbContext如何创建和初始化一个DbContext 可以在*.config文件中配置connection string,将connection string name或者将整个connection string 作为参数传递给DbContext来创建DbContext。重要配置 IgnoreIfAutoMapFails IDbContext.IgnoreIfAutoMapFails返回一个IDbContext,该实例中,如果自动映射失败时是否抛出异常通过*.config中配置的ConnectionStringName创建一个DbContext1: public IDbContext Context() 2: 3: return new DbContext().ConnectionStringName(MyDatabase, 4: new SqlServerProvider(); 5: 调用DbContext的ConnectionString方法显示设置connection string来创建 1: public IDbContext Context() 2: 3: return new DbContext().ConnectionString( 4: Server=MyServerAddress;Database=MyDatabase;Trusted_Connection=True;, new SqlServerProvider(); 5: 其他可以使用的Provider只有通过使用不同的Provider,就可以切换到不同类型的数据库服务器上,比如 AccessProvider, DB2Provider, OracleProvider, MySqlProvider, PostgreSqlProvider, SqliteProvider, SqlServerCompact, SqlAzureProvider, SqlServerProvider.FluentData入门(三)-Query查询一组数据 返回一组dynamic对象(new in .NET 4.0) 1: List products = Context.Sql(select * from Product).QueryMany();返回一组强类型对象 1: List products = Context.Sql(select * from Product).QueryMany();返回一个自定义的Collection 1: ProductionCollection products = Context.Sql(select * from Product).QueryMany();返回单个对象 返回一个dynamic对象 1: dynamic product = Context.Sql(select * from Product where ProductId = 1).QuerySingle();返回一个强类型对象: 1: Product product = Context.Sql(select * from Product where ProductId = 1).QuerySingle();返回一个DataTable: 1: DataTable products = Context.Sql(select * from Product).QuerySingle();其实QueryMany和QuerySingle都可以用来返回DataTable,但考虑到QueryMany返回的是List,所以使用QuerySingle来返回DataTable更方便。 查询一个标量值 1: int numberOfProducts = Context.Sql(select count(*) from Product).QuerySingle();返回一组标量值 1: List productIds = Context.Sql(select ProductId from Product).QueryMany();设置查询参数: 索引顺序形式的参数 1: dynamic products = Context.Sql(select * from Product where ProductId = 0 or ProductId = 1, 1, 2).QueryMany();或者 1: dynamic products = Context.Sql(select * from Product where ProductId = 0 or ProductId = 1).Parameters(1, 2).QueryMany();名字形式的参数: 1: var command = Context.Sql(select ProductName = Name from Product 2: where ProductId=1) 3: .ParameterOut(ProductName, DataTypes.String, 100); 4: command.Execute(); 5: 6: string productName = command.ParameterValue(ProductName); 7: 8: List of parameters - in operator: 9: List ids = new List() 1, 2, 3, 4 ; 10: 11: dynamic products = Context.Sql(select * from Product 12: where ProductId in(0), ids).QueryMany();Output 参数: 1: dynamic products = Context.Sql(select * from Product 2: where ProductId = ProductId1 or ProductId = ProductId2) 3: .Parameter(ProductId1, 1) 4: .Parameter(ProductId2, 2) 5: .QueryMany();FluentData入门(四)-Mapping映射 自动映射 在数据库对象和.Net object自动进行1:1匹配 1: List products = Context.Sql(select * 2: from Product) 3: .QueryMany();自动映射到一个自定义的Collection: 1: ProductionCollection products = Context.Sql(select * from Product).QueryMany();如果数据库字段和POCO类属性名不一致,使用SQL别名语法AS: 1: List products = Context.Sql(select p.*, 2: c.CategoryId as Category_CategoryId, 3: c.Name as Category_Name 4: from Product p 5: inner join Category c on p.CategoryId = c.CategoryId) 6: .QueryMany();在这里p.*中的ProductId和ProductName会自动映射到Prodoct.ProductId和Product.ProductName,而Category_CategoryId和Category_Name 将映射到 Product.Category.CategoryId 和 Product.Category.Name.使用dynamic自定义映射规则 1: List products = Context.Sql(select * from Product) 2: .QueryMany(Custom_mapper_using_dynamic); 3: 4: public void Custom_mapper_using_dynamic(Product product, dynamic row) 5: 6: product.ProductId = row.ProductId; 7: product.Name = row.Name; 8: 使用datareader进行自定义映射: 1: List products = Context.Sql(select * from Product) 2: .QueryMany(Custom_mapper_using_datareader); 3: 4: public void Custom_mapper_using_datareader(Product product, IDataReader row) 5: 6: product.ProductId = row.GetInt32(ProductId); 7: product.Name = row.GetString(Name); 8: 或者,当你需要映射到一个复合类型时,可以使用QueryComplexMany或者QueryComplexSingle。 1: var products = new List(); 2: Context.Sql(select * from Product).QueryComplexMany(products, MapComplexProduct); 3: 4: private void MapComplexProduct(IList products, IDataReader reader) 5: 6: var product = new Product(); 7: product.ProductId = reader.GetInt32(ProductId); 8: product.Name = reader.GetString(Name); 9: products.Add(product); 10: 多结果集FluentData支持多结果集。也就是说,可以在一次数据库查询中返回多个查询结果。使用该特性的时候,记得使用类似下面的语句对查询语句进行包装。需要在查询结束后把连接关闭。 1: using (var command = Context.MultiResultSql) 2: 3: List categories = command.Sql( 4: select * from Category; 5: select * from Product;).QueryMany(); 6: 7: List products = command.QueryMany(); 8: 执行第一个查询时,会从数据库取回数据,执行第二个查询的时候,FluentData可以判断出这是一个多结果集查询,所以会直接从第一个查询里获取需要的数据。分页 1: List products = Context.Select(p.*, c.Name as Category_Name) 2: .From(Product p 3: inner join Category c on c.CategoryId = p.CategoryId) 4: .Where(p.ProductId 0 and p.Name is not null) 5: .OrderBy(p.Name) 6: .Paging(1, 10).QueryMany();调用 Paging(1, 10),会返回最先检索到的10个Product。FluentData入门(五)Insert, Update, Delete插入数据 使用 SQL 语句: 1: int productId = Context.Sql(insert into Product(Name, CategoryId) 2: values(0, 1);) 3: .Parameters(The Warren Buffet Way, 1) 4: .ExecuteReturnLastId();使用builder: 1: int productId = Context.Insert(Product) 2: .Column(Name, The Warren Buffet Way) 3: .Column(CategoryId, 1) 4: .ExecuteReturnLastId();使用builder,并且自动映射 1: Product product = new Product(); 2: product.Name = The Warren Buffet Way; 3: product.CategoryId = 1; 4: 5: product.ProductId = Context.Insert(Product, product) 6: .AutoMap(x = x.ProductId) 7: .ExecuteReturnLastId(); 8: 将ProductId作为AutoMap方法的参数,是要指明ProductId不需要进行映射,因为它是一个数据库自增长字段。更新数据 使用SQL语句: 1: int rowsAffected = Context.Sql(update Product set Name = 0 2: where ProductId = 1) 3: .Parameters(The Warren Buffet Way, 1) 4: .Execute();使用builder: 1: int rowsAffected = Context.Update(Product) 2: .Column(Name, The Warren Buffet Way) 3: .Where(ProductId, 1) 4: .Execute();使用builder,并且自动映射: 1: Product product = Context.Sql(select * from Product 2: where ProductId = 1) 3: .QuerySingle(); 4: product.Name = The Warren Buffet Way; 5: 6: int rowsAffected = Context.Update(Product, product) 7: .AutoMap(x = x.ProductId) 8: .Where(x = x.ProductId) 9: .Execute();将ProductId作为AutoMap方法的参数,是要指明ProductId不需要进行映射,因为它不需要被更新。Insert and update - common Fill method 1: var product = new Product(); 2: product.Name = The Warren Buffet Way; 3: product.CategoryId = 1; 4: 5: var insertBuilder = Context.Insert(Product, product).Fill(FillBuilder); 6: 7: var updateBuilder = Context.Update(Product, product).Fill(FillBuilder); 8: 9: public void FillBuilder(IInsertUpdateBuilder builder) 10: 11: builder.Column(x = x.Name); 12: builder.Column(x = x.CategoryId); 13: 14: 15: Delete删除数据 使用SQL语句: 1: int rowsAffected = Context.Sql(delete from Product 2: where ProductId = 1) 3: .Execute();使用builder: 1: int rowsAffected = Context.Delete(Product) 2: .Where(ProductId, 1) 3: .Execute();FluentData入门(六)-存储过程和事务存储过程使用SQL语句: 1: var rowsAffected = Context.Sql(ProductUpdate) 2: .CommandType(DbCommandTypes.StoredProcedure) 3: .Parameter(ProductId, 1) 4: .Parameter(Name, The Warren Buffet Way) 5: .Execute();使用builder: 1: var rowsAffected = Context.StoredProcedure(ProductUpdate) 2: .Parameter(Name, The Warren Buffet Way) 3: .Parameter(ProductId, 1).Execute();使用builder,并且自动映射 1: var product = Context.Sql(select * from Product where ProductId = 1) 2: .QuerySingle(); 3: 4: product.Name = The Warren Buffet Way; 5: 6: var rowsAffected = Context.StoredProcedure(ProductUpdate, product) 7: .AutoMap(x = x.CategoryId).Execute
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程项目勘察管理信息化方案
- 城市建设项目消防安全管理方案
- 项目安全监控与预警方案
- 燃气系统设备选型与安装方案
- 房屋施工技术交底与培训方案
- 2025巢湖水业招聘考试真题及答案
- 园林古建筑污水处理技术方案
- 2025测绘师考试真题及答案
- 2025沧州医专教师考试真题及答案
- 2025彩虹社工考试真题及答案
- 医院应急知识培训课件
- 2025-2030中国抗骨质疏松药物市场调研及未来增长预测报告
- 终极焊工考试试题及答案
- 房屋安全性鉴定培训试题及答案解析
- 2025广西南宁上林县公安局面向社会招聘警务辅助人员50人笔试备考试题及答案解析
- 火锅店引流截流回流方案
- 黑龙江省齐齐哈尔市富拉尔基区2024-2025学年高一上学期期中考试生物试题含参考答案
- 2025年档案员考试试题及答案
- 2025年4月自考03450公共部门人力资源管理试题
- (高清版)DZT 0399-2022 矿山资源储量管理规范
- 前置胎盘PPT(共31张PPT)课件
评论
0/150
提交评论