• C# SqlSugar ORM管理数据


    1、连接数据库

    1. using SqlSugar;
    2. namespace BooksDataCenter
    3. {
    4. ///
    5. /// ORM 基础仓储
    6. ///
    7. ///
    8. public class Repository<T> : SimpleClient<T> where T : class, new()
    9. {
    10. public Repository(ISqlSugarClient context = null) : base(context)
    11. {
    12. if (context == null)
    13. {
    14. //数据库连接字符串
    15. string ConnectionString = ConfigurationManager.AppSettings["SqlConnectionStr"];
    16. ConnectionString += "Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;packet size=1000;MultipleActiveResultSets = True";
    17. base.Context = new SqlSugarClient(
    18. new ConnectionConfig()
    19. {
    20. ConnectionString = ConnectionString,
    21. DbType = DbType.SqlServer,
    22. IsAutoCloseConnection = true,
    23. InitKeyType = InitKeyType.Attribute
    24. }
    25. );
    26. }
    27. }
    28. public ISqlSugarClient db
    29. {
    30. get { return base.Context; }
    31. }
    32. }
    33. }

    2、定义

    1. using SqlSugar;
    2. namespace BooksDataCenter
    3. {
    4. [Serializable]
    5. [SugarTable("管理员")]
    6. public class BookAdmin
    7. {
    8. [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    9. public bool 字段1 { get; set; }
    10. public bool 字段2 { get; set; }
    11. public bool 字段3 { get; set; }
    12. public bool 字段4 { get; set; }
    13. public bool 字段5 { get; set; }
    14. }
    15. }
    1. using SqlSugar;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Linq.Expressions;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace BooksDataCenter
    9. {
    10. public class BookInfoService : Repository<BookInfo>
    11. {
    12. //分页检索
    13. public async Task> ListPage(string key, int page, int rows)
    14. {
    15. RefAsync<int> totalCount = 0;
    16. var exp = Expressionable.Create();
    17. if (!string.IsNullOrEmpty(key))
    18. {
    19. exp.Or(it => it..Contains(key));
    20. exp.Or(it => it.字段1.Contains(key));
    21. exp.Or(it => it.字段2.Contains(key));
    22. exp.Or(it => it.字段3.Contains(key));
    23. exp.Or(it => it.字段4.Contains(key));
    24. exp.Or(it => it.字段5.Contains(key));
    25. }
    26. var result = await db.Queryable()
    27. .Where(exp.ToExpression())
    28. .OrderBy(l => l.ID, OrderByType.Desc)
    29. .ToPageListAsync(page, rows, totalCount);
    30. return new PagedResult(rows, totalCount, page, result);
    31. }
    32. //分页检索 同步
    33. public PagedResult ListPageTwoMy(string key, int page, int rows)
    34. {
    35. int totalCount = 0;
    36. var exp = Expressionable.Create();
    37. if (!string.IsNullOrEmpty(key))
    38. {
    39. exp.Or(it => it..Contains(key));
    40. exp.Or(it => it.字段1.Contains(key));
    41. exp.Or(it => it.字段2.Contains(key));
    42. exp.Or(it => it.字段3.Contains(key));
    43. exp.Or(it => it.字段4.Contains(key));
    44. exp.Or(it => it.字段5.Contains(key));
    45. }
    46. var result = db.Queryable()
    47. .Where(exp.ToExpression())
    48. .OrderBy(l => l.ID, OrderByType.Desc)
    49. .ToPageList(page, rows, ref totalCount);
    50. return new PagedResult(rows, totalCount, page, result);
    51. }
    52. //更新列
    53. public Task<int> UpdateLine(BookInfo entity, Expressionobject>> columns, Expressionobject>> expression)
    54. {
    55. return db.Updateable(entity).UpdateColumns(columns).WhereColumns(expression).ExecuteCommandAsync();
    56. }
    57. }
    58. }

  • 相关阅读:
    k8s--基础--29.3--ingress--案例
    C++征途 --- 内建函数对象
    CSP认证202305-2矩阵运算(c++)
    Shell脚本函数简介及运用
    docker常用命令解析
    数据库管理工具DBeaverUltimate mac中文高级功能
    阿里云服务器修改IP地址的两种方法
    Servlet
    优质笔记软件综合评测和详细盘点 Notion、Obsidian、RemNote、FlowUs
    二、Node.js内置API(fs、path、http)
  • 原文地址:https://blog.csdn.net/usdnfo/article/details/127732922