1、下载
这里需要注意的是,FluentData需要.NET4.0的支持,同时支持dynamic,支持的数据库有:
MS SQL Server
MS SQL Server Compact 4.0
MS SQL Azure
MS Access
Oracle
MySQL
SQLite
PostgreSQL
IBM DB2
Sybase
2、使用
说说简单的增、删、改、查,创建并且初始化一个IDbContext,函数中第一个参数为数据库链接字符串,
第二个参数为数据库类型
- public static IDbContext Context()
- {
- var context = new DbContext().ConnectionString(ConnectionString, new SqlServerProvider());
- return context;
- }
- 新增使用:
- var MembershipId = FluentDataHelper.Context().Sql("insert into Membership(Name,Address) values(@0,@1)").Parameters("张三", "长沙").ExecuteReturnLastId<int>();
- Or
- 复制代码
- var Insert = FluentDataHelper.Context().Insert("Membership")
- .Column("Name", Name)
- .Column("Address", Address);
-
- if (ImageByte != null)
- Insert.Column("Image", ImageByte);
- int MembershipId = Insert.ExecuteReturnLastId<int>();
复制代码
复制代码
删除使用:
- int RowId = FluentDataHelper.Context().Sql("delete Membership where Id = @Id").Parameter("Id", Id).Execute();
- Or
- int RowId = FluentDataHelper.Context().Delete("Membership").Where("Id", Id).Execute();
- 修改使用:
- int RowId = FluentDataHelper.Context().Sql("update Membership set Name = @Name where Id = @Id").Parameter("Name", Name).Parameter("Id", EditId).Execute();
- Or
- 复制代码
- var Update = FluentDataHelper.Context().Update("Membership")
- .Column("Name", Name)
- .Where("Id", EditId);
- if (ImageByte != null)
- Update.Column("Image", ImageByte);
- int RowId = Update.Execute();
复制代码
复制代码
查询使用:
复制代码
//返回单个值
- int RowId = FluentDataHelper.Context().Select<int>("Count(1)")
- .From("Membership")
- .Where("Tel = @Tel").Parameter("Tel", Tel)
- .AndWhere("Id != @Id").Parameter("Id", EditId)
- .QuerySingle();
- 复制代码
- //返回实体
- MembershipEntity Entity = FluentDataHelper.Context().Select<MembershipEntity>("*")
- .From("Membership")
- .Where("Id = @Id").Parameter("Id", EditId)
- .QuerySingle();
- //返回多个对象
- List<MembershipGroupEntity> ListGroup = FluentDataHelper.Context().Select<MembershipGroupEntity>("*")
- .From("MembershipGroup")
- .QueryMany();
- 复制代码
- //多表关联及分页
- var select = FluentDataHelper.Context()
- .Select<VehicleEntity>("Vehicle.*,AdminUser.Name as AdminName,Membership.Name as MembershipName,Membership.Tel,Membership.CardId,Membership.Address,Membership.Source")
- .From("Vehicle left join AdminUser on Vehicle.AdminId = AdminUser.Id left join Membership on Vehicle.MembershipId = Membership.Id")
- .OrderBy("Vehicle.Time desc")
- .Paging(pageInfo.PageIndex, pageInfo.PageSize);
- if (!string.IsNullOrEmpty(Name))
- {
- select.AndWhere("Membership.Name like @Name").Parameter("Name", string.Format("%{0}%", Name));
- }
- if (!string.IsNullOrEmpty(Code))
- {
- select.AndWhere("Code = @Code").Parameter("Code", Code);
- }
- this.GridViewVehicle.DataSource = select.QueryMany();
复制代码
复制代码
事务使用:
复制代码
//反生异常,事务自动回滚
- using (var context = FluentDataHelper.Context().UseTransaction(true))
- {
- context.Sql("update Membership set Name = @0 where Id = @1")
- .Parameters("李四", 12)
- .Execute();
- context.Sql("update Product set Name = @0 where Id = @1")
- .Parameters("Product不存在数据库中", 1)
- .Execute();
- context.Commit();
- }
复制代码
复制代码
好了,基本简单的使用到此就已经结束了,如果有兴趣的朋友可以去看看这个的源码,这个是开源的,而且拓
展性也是很不错的。
|