当前位置:   article > 正文

Dapper学习入门三:扩展库DapperExtensions

dapperextensions

Dapper学习入门二:https://blog.csdn.net/czjnoe/article/details/106454606

DotTrace学习入门之一:https://blog.csdn.net/czjnoe/article/details/106438270

 

介绍DapperExtensions:

DapperExtensions是Dapper的简单的扩展类库,对增删改查进行了静态方法的扩展。可以少写些SQL语句

 

NuGet下载:

实战:

  1. Random rand = new Random();
  2. using (IDbConnection db = DapperFactory.GetConnection(Enums.MyDbType.SqlServer, "Data Source=localhost;Initial Catalog=test;Integrated Security=True"))
  3. {
  4. string id = rand.Next(1, 2000000000).ToString();
  5. Student stu = new Student { ID = id, NAME = "陈兆杰", AGE = 25, TIME = DateTime.Now };
  6. //Insert
  7. {
  8. var effectRows = db.Insert(stu);//返回主键值
  9. }
  10. //Update
  11. {
  12. stu.TIME = DateTime.Now;
  13. if (db.Update(stu))
  14. {
  15. //成功
  16. }
  17. else
  18. {
  19. //失败
  20. }
  21. }
  22. //删除
  23. {
  24. //方法一
  25. {
  26. //if (db.Delete(stu))
  27. //{
  28. // //成功
  29. //}
  30. //else
  31. //{
  32. // //失败
  33. //}
  34. }
  35. //方法二
  36. {
  37. var filedPred = Predicates.Field<Student>(o => o.ID, Operator.Eq, stu.ID);
  38. if (db.Delete<Student>(filedPred))
  39. {
  40. //成功
  41. }
  42. else
  43. {
  44. //失败
  45. }
  46. }
  47. //查询list
  48. {
  49. IList<ISort> sortlist = new List<ISort>();
  50. sortlist.Add(new Sort { PropertyName = "ID", Ascending = false });//排序条件 降序
  51. IList<IPredicate> preList = new List<IPredicate>();
  52. preList.Add(Predicates.Field<Student>(o => o.ID, Operator.Eq, "1786863176"));//搜索条件,Operator有很多种的类型如eq是等于、like相当于是sql的like用法还有Lt、Le等
  53. BetweenValues betweenValues = new BetweenValues();//搜索条件,between搜索两个值之间的数据
  54. betweenValues.Value1 = "1786863176";
  55. betweenValues.Value2 = "9786863176";
  56. preList.Add(Predicates.Between<Student>(o => o.ID, betweenValues));
  57. IPredicateGroup predGroup = Predicates.Group(GroupOperator.And, preList.ToArray());//确认多个搜索条件的连接方式AND 或者 OR
  58. var list = db.GetList<Student>(predGroup, sortlist).ToList();
  59. }
  60. }
  61. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/135152
推荐阅读
相关标签
  

闽ICP备14008679号