赞
踩
Dapper学习入门二:https://blog.csdn.net/czjnoe/article/details/106454606
DotTrace学习入门之一:https://blog.csdn.net/czjnoe/article/details/106438270
DapperExtensions是Dapper的简单的扩展类库,对增删改查进行了静态方法的扩展。可以少写些SQL语句。
- Random rand = new Random();
- using (IDbConnection db = DapperFactory.GetConnection(Enums.MyDbType.SqlServer, "Data Source=localhost;Initial Catalog=test;Integrated Security=True"))
- {
- string id = rand.Next(1, 2000000000).ToString();
- Student stu = new Student { ID = id, NAME = "陈兆杰", AGE = 25, TIME = DateTime.Now };
- //Insert
- {
- var effectRows = db.Insert(stu);//返回主键值
- }
-
- //Update
- {
- stu.TIME = DateTime.Now;
- if (db.Update(stu))
- {
- //成功
- }
- else
- {
- //失败
- }
- }
-
- //删除
- {
- //方法一
- {
- //if (db.Delete(stu))
- //{
- // //成功
- //}
- //else
- //{
- // //失败
- //}
- }
-
- //方法二
- {
- var filedPred = Predicates.Field<Student>(o => o.ID, Operator.Eq, stu.ID);
- if (db.Delete<Student>(filedPred))
- {
- //成功
- }
- else
- {
- //失败
- }
- }
-
- //查询list
- {
- IList<ISort> sortlist = new List<ISort>();
- sortlist.Add(new Sort { PropertyName = "ID", Ascending = false });//排序条件 降序
-
- IList<IPredicate> preList = new List<IPredicate>();
- preList.Add(Predicates.Field<Student>(o => o.ID, Operator.Eq, "1786863176"));//搜索条件,Operator有很多种的类型如eq是等于、like相当于是sql的like用法还有Lt、Le等
-
- BetweenValues betweenValues = new BetweenValues();//搜索条件,between搜索两个值之间的数据
- betweenValues.Value1 = "1786863176";
- betweenValues.Value2 = "9786863176";
- preList.Add(Predicates.Between<Student>(o => o.ID, betweenValues));
-
- IPredicateGroup predGroup = Predicates.Group(GroupOperator.And, preList.ToArray());//确认多个搜索条件的连接方式AND 或者 OR
- var list = db.GetList<Student>(predGroup, sortlist).ToList();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。