k.dbtool.engine v1.0.0.5
http://k-chen.me
帮助开发人员提升开发效率,使开发人员能更加关注于数据模型的逻辑处理。使代码更加精炼,减少30%数据访问层代码量。
支持的环境
.Net Framework 4.0 +
Ms-Sql 2005 +
使用说明
1. 使用命令行生成工具k.dbtool.cmdLine.exe 生成数据模型(配置参考3)
2. 项目中添加 k.dbtool.engine.dll
3.项目web.config中添加数据连接:
1 <appSettings> 2 <add key="k.dbtool.engine.connstr" value="数据库连接"/> 3 </appSettings>
OK,配置工作已经完成!接下来高大上的代码编写方式!
Hello World示例
public class HelloWoldModel { public string Hi { get; set; } } //自动生成的Model
var dyQuery = new DyQuery<HelloWoldModel>(); Dy dy = new Dy(); dy.Query(dyQuery).AsJson() ;//返回json格式的数据 //or dy.Query(dyQuery).AsT<HelloWold>();//返回单个实体对象 //or dy.Query(dyQuery).AsTList<HelloWold>();//返回单个实体对象列表 //or dy.QueryPaged(dyQuery,pageIndex,pageSize).AsPageList<HelloWold>( pageIndex,pageSize);//返回带分页的单个实体对象 //or dy.Query(dyQuery).ToString();//生成的查询语句
核心类:
DyQuery <TLeft>:查询接口
Dy:查询操作类(正真与数据库进行交互)
EngineResult:数据查询返回的结果
须知:Where条件实现的系统函数(SQL系统函数的映射),使用的时候不要使用C#原生的一些函数,如Contains->应该使用DyContains
"DyContains", "DyStartsWith", "DyEndsWith","DySubstring","DyLen","DyReplace","DyNotContains",
"DyNotStartsWith","DyNotEndsWith","DyStuff","DyCharIndex","DyLeft","DyRight","DyT","DyMax",
"DyMin","DySum","DyAvg","DyCount","DyCount1","DyStrGT","DyStrGE","DyStrLT","DyStrLE",
"ToString"
操作示例代码
查询操作(Select)
public void 单表查询() { var dyQuery = new DyQuery<Tb_Stock_Rzrq>() .Where(t => t.股票代码.DyContains("03") && t.融资买入额 > 1000000m && t.股票代码 == "601866" /*&& t.日期 >= DateTime.Now.DyT("yyyy-MM-dd")*/) .Order(OrderWay.Desc, t => t.股票代码).Select(t => new {t.股票名称,t.股票代码 }).AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.ReadLine(); } public void 多表查询() { string key0 = "03"; string key1 = "30"; string 交易所 = "sz"; var dyQuery = new DyQuery<Tb_Stock_Rzrq>() .Join<Tb_Stock_Trade>(JoinWay.InnerJoin, (t1, t2) => t1.股票代码 == t2.股票代码 && t1.交易日期 == t2.交易日期) .Where(t => t.股票代码.DyNotContains(key0) && t.股票代码.DyContains(key1)) .Where<Tb_Stock_Rzrq>(t => t.交易所 == 交易所 && t.融券余额 > 1000000) .Order<Tb_Stock_Rzrq, string>(OrderWay.Desc, t => t.股票代码) .Select<Tb_Stock_Trade>(t => new { t.股票代码, t.股票名称, t.成交量 }) .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.ReadLine(); } public void 子查询() { List<string> whereList = new List<string> { "300151", "300152", "300153", "300154", }; var subQuery1 = new DyQuery<Tb_Stock_Rzrq>() .Where(ConnectWay.And, t => t.股票代码.DySubstring(3, 2) == "01") .Select(t => new { 股票代码 = t.股票代码.DySubstring(2, 3) }) .AsQuery(); var subQuery2 = new DyQuery<Tb_Stock_Rzrq>() .Where(t => t.股票代码.DyContains("30")) .Select(t => new { t.股票代码 }) .AsQuery(); var existsQuery = new DyQuery<Tb_Stock_Rzrq>() .Where(t => t.股票代码.DyContains("30")) .Select(t => new { t.股票代码 }) .AsQuery(); var dyQuery = new DyQuery<Tb_Stock_Trade>() .WhereIn<Tb_Stock_Trade, string>(ConnectWay.And, t => t.股票代码.DySubstring(2, 3), subQuery1) .WhereNotIn<Tb_Stock_Trade, string>(ConnectWay.And, t => t.股票代码, whereList) .WhereNotIn<Tb_Stock_Trade, string>(ConnectWay.And, t => t.股票代码, subQuery2) .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.ReadLine(); } public void Exists子查询() { var subQuery = new DyQuery<Tb_Stock_Rzrq>() .Where(t => t.股票代码.DyContains("30")) .Select(t => new { 数量 = t.股票代码.DyCount1() }) .SetWhereExistsCondition<Tb_Stock_Trade, Tb_Stock_Rzrq>(ConnectWay.And, (t1, t2) => t1.特大单成交金 > t2.融资余额) .AsQuery(); var dyQuery = new DyQuery<Tb_Stock_Trade>() .WhereExists(ConnectWay.And, subQuery) .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.ReadLine(); } public void 分组查询() { var dyQuery = new DyQuery<Tb_Stock_Trade>() .Group<Tb_Stock_Trade, string>(t => t.股票代码.DySubstring(2, 3)) .Group<Tb_Stock_Trade, decimal>(t => t.今开) .Having<Tb_Stock_Trade>(ConnectWay.And, t => t.今开.DyCount() > 1) .Select<Tb_Stock_Trade>(t => new { 数量 = t.交易所.DyCount(), 最大 = t.最高.DyMax(), 最小 = t.最低.DyMin(), t.今开 }) .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.ReadLine(); } public void 分页查询() { var dyQuery = new DyQuery<Tb_Stock_Rzrq>().Where(t => t.股票代码.DyContains("03") && t.融资买入额 > 1000000m) .Order(OrderWay.Desc, t => t.股票代码).AsQuery(); var pageList = dy.QueryPaged(dyQuery, 1, 20); Console.WriteLine(pageList.AsJson()); Console.ReadLine(); } public void 记录行数() { var dyQuery = new DyQuery<Tb_Stock_Trade>().Where(t => t.今开 > 100).AsQuery(); var count = dy.Count(dyQuery); Console.WriteLine(count); Console.ReadLine(); } public void 首行首列() { var dyQuery = new DyQuery<Tb_Stock_Trade>().Where(t => t.今开 > 100).AsQuery(); var ret = dy.RC(dyQuery); Console.WriteLine(ret); Console.ReadLine(); } public void 是否存在() { var dyQuery = new DyQuery<Tb_Stock_Trade>().Where(t => t.今开 > 100).AsQuery(); var exists = dy.Exists(dyQuery); Console.WriteLine(exists); Console.ReadLine(); } public void 取Top() { var dyQuery = new DyQuery<Tb_Stock_Trade>().Where(t => t.今开 > 100).Select(t => new { t.股票名称, t.股票代码 }).AsQuery(); var result = dy.Top(dyQuery, 5); Console.WriteLine(result.AsJson()); Console.ReadLine(); }
插入操作(Insert)
public void 单行插入() { var dyQuery = new DyQuery<Tb_Sync_Opt>().Insert(new Tb_Sync_Opt { 同步日期 = "2016-01-01", 资金同步标识 = 1, 量价同步标识 = 1, 两融同步标识 = 1, 数据初始化标识 = 1, 同步完成标识 = 1, V = Guid.NewGuid().ToString("N") }).AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 多行插入() { List<Tb_Sync_Opt> opts = new List<Tb_Sync_Opt>(); opts.Add(new Tb_Sync_Opt { 同步日期 = "2015-01-29", 资金同步标识 = 1, 量价同步标识 = 1, 两融同步标识 = 1, 数据初始化标识 = 1, 同步完成标识 = 1 }); opts.Add(new Tb_Sync_Opt { 同步日期 = "2015-01-30", 资金同步标识 = 1, 量价同步标识 = 1, 两融同步标识 = 1, 数据初始化标识 = 1, 同步完成标识 = 1 }); var dyQuery = new DyQuery<Tb_Sync_Opt>().Insert(opts).Insert<Tb_Stock_Rzrq>(new Tb_Stock_Rzrq { 交易日期 = "2015-01-29", 股票名称 = "中海集运", 股票代码 = "601866" }).AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); }
更新操作(Update)
public void 单表按需更新() { var dyQuery = new DyQuery<Tb_Sync_Opt>().Update(t => t.两融同步标识 == 1).AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 单表条件更新() { var dyQuery = new DyQuery<Tb_Sync_Opt>().Update(t => t.量价同步标识 == t.量价同步标识 + 5).Where(t => t.同步日期 == "2016-01-30").AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 他表更新() { var dyQuery = new DyQuery<Tb_Sync_Opt>().UpdateFrom<Tb_Stock_Trade>((left, from) => left.V == from.股票代码 && left.两融同步标识 == 0) .Where<Tb_Stock_Trade>(t => t.交易日期 == "2015-12-01" && t.股票代码 == "601866") .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 多表连接更新() { var dyQuery = new DyQuery<Tb_Sync_Opt>().UpdateFrom<Tb_Stock_Trade>((left, from) => left.V == from.股票代码 && left.两融同步标识 == 0) .Join<Tb_Stock_Rzrq>(JoinWay.InnerJoin, (t1, t2) => t1.同步日期 == t2.交易日期) .Join<Tb_Stock_Rzrq, Tb_Stock_Trade>(JoinWay.InnerJoin,(t1,t2)=> t1.交易日期==t2.交易日期 && t1.股票代码 == t2.股票代码) .Where<Tb_Stock_Trade>(t => t.交易日期 == "2015-12-01" && t.股票代码 == "601866") .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 实例更新() { var dy1 = new DyQuery<Tb_Sync_Opt>().Where(t => t.同步日期.DyStrLE("2015-12-03")).Select().AsQuery(); var dy2 = new DyQuery<Tb_Stock_Trade>().Where(t => t.交易日期 == "2015-12-01" && t.股票代码.DyContains("60166")).Select().AsQuery(); List<Tb_Sync_Opt> opts = dy.Query(dy1).AsTList<Tb_Sync_Opt>(); foreach (var opt in opts) { opt.两融同步标识 = 1; opt.量价同步标识 = 1; } List<Tb_Stock_Trade> trades = dy.Query(dy2).AsTList<Tb_Stock_Trade>(); int i=0; foreach (var trade in trades) { trade.股票名称 = trade.股票名称 + 2; } var dyQuery = new DyQuery<Tb_Sync_Opt>().Update(opts).Update(trades).AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); }
删除操作(Delete)
public void 直接删除() { var dyQuery = new DyQuery<Tb_Sync_Opt>().Delete().AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 单表条件删除() { var dyQuery = new DyQuery<Tb_Sync_Opt>().Delete().Where(t => t.同步日期 == "2016-01-29").AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 他表删除() { var existsQuery = new DyQuery<Tb_Stock_Trade>() .SetWhereExistsCondition<Tb_Stock_Rzrq, Tb_Stock_Trade>(ConnectWay.And, (parent, child) => parent.股票代码 == child.股票代码) .Select(t => 1).AsQuery(); var dyQuery = new DyQuery<Tb_Stock_Rzrq>().Delete() .WhereIn<string>(ConnectWay.And, t => t.股票代码, new List<string> { "000001", "000002" }) .WhereExists(ConnectWay.And,existsQuery) .AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); } public void 实体删除() { var dy1 = new DyQuery<Tb_Sync_Opt>().Where(t => t.同步日期.DyStrLE("2015-12-03")).Select().AsQuery(); var dy2 = new DyQuery<Tb_Stock_Trade>().Where(t => t.交易日期.DyStrLE("2015-12-03") && t.股票名称=="test").Select().AsQuery(); List<Tb_Sync_Opt> opts = dy.Query(dy1).AsTList<Tb_Sync_Opt>(); List<Tb_Stock_Trade> trades = dy.Query(dy2).AsTList<Tb_Stock_Trade>(); var dyQuery = new DyQuery<Tb_Sync_Opt>().Delete(opts).Delete(trades).AsQuery(); Console.WriteLine(dy.Query(dyQuery).AsJson()); Console.Read(); }
存储过程操作(Storage)
public void 不带参数SP() { var dy1 = new DyQuery<SP_TB_SYNC_OPT_QUERY_ALL>().Proc(null).Proc<SP_TB_ESTIMATE_INDEXES_QUERY_ALL>(null).AsQuery(); Console.WriteLine(dy.Query(dy1).AsJson()); Console.Read(); } public void 带参数SP() { var dy1 = new DyQuery<SP_TB_VIP_QUERY_ONE>().Proc(new SP_TB_VIP_QUERY_ONE { 会员ID = "dcf9e93390dd0bfe" }) .Proc<SP_TB_CAPTCHA_RULE_QUERY_ONE>(new SP_TB_CAPTCHA_RULE_QUERY_ONE { RULEID = "1e051f49975f46dbbb7fd5e554c70385" }) .Proc<SP_TB_ESTIMATE_INDEXES_QUERY_ONE>(new SP_TB_ESTIMATE_INDEXES_QUERY_ONE { 交易日期="2015-12-24", 股票代码="002242"}) .AsQuery(); Console.WriteLine(dy.Query(dy1).AsJson()); Console.Read(); }
Insert,Update,Delete操作自动事务处理
数据版本控制方式,在数据库创建表的时候,添加字段名 V varchar(36) 框架会自动进行版本控制(在Insert,Update,Delete操作中)