Bouyei.DbFactory.net 多种数据库统一接口
开发库主要实现功能多个数据库统一接口,CRUD简化快速使用,类似mybatis框架,具体功能如下:
一、
1)、.net 统一数据库接口库,支持 SQL server,DB2,Oracle,MySQL,Postgresql,access,sqlite 等;
2)、支持.net Core 2.0,.net framework,.net standard 2.0+
3)、sql脚本简单Linq条件表达式的CRUD操作
4)、实体映射赋值
5)、单表分页查询
二、例子
- sql 脚本增删查改生成例子
//生成简单查询脚本
ISqlProvider sqlProvider = SqlProvider.CreateProvider();
//group by
string sqlgroupby = sqlProvider.Select<User>().Count().From<User>()
.Where(x => x.uage == 1).GroupBy<User>().SqlString;
//function
string sqlfun = sqlProvider.Select<User>(new Max("age")).From<User>().Where(x=>x.uage>20).SqlString;
//order by
var osql = sqlProvider.Select<User>().From<User>().OrderBy(SortType.Asc, "name").SqlString;
var dic = new Dictionary<string, object>();
dic.Add("name", "hellow");
dic.Add("age", 0);
dic.Add("score", 1.0);
//MappedName 测试
var sqls = sqlProvider.Insert<UserDto>().Values(new UserDto[] { new UserDto() {
Pwd="ds",
UserName="d"
} }).SqlString;
//查询
var sql = sqlProvider.Select<User>()
.From().Where(x => x.id == 1).Top(FactoryType.PostgreSQL, 10).SqlString;
//修改
sql = sqlProvider.Update<User>()
.Set(new User() { uname = "bouyei" })
.Where<User>(x => x.id == 1 || (x.uname == "b" && x.score == 2)).SqlString;
//删除
sql = sqlProvider.Delete()
.From<User>().Where(x => x.uname == "bouyei").SqlString;
//插入
sql = sqlProvider.Insert<User>()
.Values(new User[] {
new User() { uname ="hello", uage=12 }
,new User() { uname="bouyei",uage=23} }).SqlString;
2、增删查改操作例子
IAdoProvider dbProvider = AdoProvider.CreateProvider(connectionString, FactoryType.PostgreSQL);
var ext = dbProvider.Connect(connectionString);
//原生脚本执行
var adort = dbProvider.Query(new Parameter()
{
CommandText = "select * from public.db_user"
});
//查询
var rt= dbProvider.Query<User>(x => x.uage >= 20);
//单表分页 查询语法
var takert = dbProvider.PageQuery<User>(x => x.uage == 30,0, 10);
//更改
var dic = new Dictionary<string, object>();
dic.Add("name", "hellow");
dic.Add("age", 0);
dic.Add("score", 1.0);
dbProvider.Update<User>(dic, x => x.id == 1);
//删除操作
dbProvider.Delete<User>(x => x.uname == "bouyei");
//插入
var users = new User[] {
new User(){ uname="bouyei", score=100, uage=30 },
new User(){ uname="八渡", score=10, uage=20 }
};
dbProvider.Insert(users);
3、mapper实体,实体对象复制到新对象
User u = new User() {
uname="b",
id=1
};
User b = new User() {
uname="a",
id=2
};
//u对象定义的变量复制给b对象,条件是只复制name等变量名
EntityMapper.MapTo(u, b, FilterType.Include, "name");
4、实体或服务继承实现基本的增删查改操作无需写sql脚本
[MappedName("db_user")]
public class UserDto : BaseMapper<UserDto>
{
public string UserName { get; set; }
[Ignore]
public string Pwd { get; set; }
public void AddUser(UserDto user)
{
base.Insert(user);
}
public void DeleteById(string name)
{
base.Delete(x => x.UserName == name);
}
public void UpdateUser(UserDto dto)
{
base.Update(dto, x => x.UserName == "bouyei");
}
public List<UserDto> QueryUsers(int page=0,int size=10)
{
return base.Select(page, size, x => true);
}
}
public class fc3d:BaseMapper<fc3d>
{
public string fname { get; set; }
public long fcode { get; set; }
}
public class BaseMapper<T> : TableMapper<T> where T : class
{
public BaseMapper()
{
string connstr = "Host=127.0.0.1;Port=5432;User id=postgres;Password=bouyei;Database=postgres;";
var provider = AdoProvider.CreateProvider(connstr, FactoryType.PostgreSQL);
Initialized(provider);
}
}