1、连接数据库
- using SqlSugar;
- namespace BooksDataCenter
- {
- ///
- /// ORM 基础仓储
- ///
- ///
- public class Repository<T> : SimpleClient<T> where T : class, new()
- {
- public Repository(ISqlSugarClient context = null) : base(context)
- {
- if (context == null)
- {
- //数据库连接字符串
- string ConnectionString = ConfigurationManager.AppSettings["SqlConnectionStr"];
-
- ConnectionString += "Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;packet size=1000;MultipleActiveResultSets = True";
- base.Context = new SqlSugarClient(
- new ConnectionConfig()
- {
- ConnectionString = ConnectionString,
- DbType = DbType.SqlServer,
- IsAutoCloseConnection = true,
- InitKeyType = InitKeyType.Attribute
- }
- );
- }
- }
-
- public ISqlSugarClient db
- {
- get { return base.Context; }
- }
- }
- }
2、定义
- using SqlSugar;
-
- namespace BooksDataCenter
- {
- [Serializable]
- [SugarTable("管理员")]
-
- public class BookAdmin
- {
- [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
- public bool 字段1 { get; set; }
- public bool 字段2 { get; set; }
- public bool 字段3 { get; set; }
- public bool 字段4 { get; set; }
- public bool 字段5 { get; set; }
- }
-
- }
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BooksDataCenter
- {
- public class BookInfoService : Repository<BookInfo>
- {
- //分页检索
- public async Task
> ListPage(string key, int page, int rows) - {
- RefAsync<int> totalCount = 0;
- var exp = Expressionable.Create
(); - if (!string.IsNullOrEmpty(key))
- {
- exp.Or(it => it..Contains(key));
- exp.Or(it => it.字段1.Contains(key));
- exp.Or(it => it.字段2.Contains(key));
- exp.Or(it => it.字段3.Contains(key));
- exp.Or(it => it.字段4.Contains(key));
- exp.Or(it => it.字段5.Contains(key));
- }
- var result = await db.Queryable
() - .Where(exp.ToExpression())
- .OrderBy(l => l.ID, OrderByType.Desc)
- .ToPageListAsync(page, rows, totalCount);
- return new PagedResult
(rows, totalCount, page, result); - }
-
- //分页检索 同步
- public PagedResult
ListPageTwoMy(string key, int page, int rows) - {
- int totalCount = 0;
- var exp = Expressionable.Create
(); - if (!string.IsNullOrEmpty(key))
- {
-
- exp.Or(it => it..Contains(key));
- exp.Or(it => it.字段1.Contains(key));
- exp.Or(it => it.字段2.Contains(key));
- exp.Or(it => it.字段3.Contains(key));
- exp.Or(it => it.字段4.Contains(key));
- exp.Or(it => it.字段5.Contains(key));
- }
- var result = db.Queryable
() - .Where(exp.ToExpression())
- .OrderBy(l => l.ID, OrderByType.Desc)
- .ToPageList(page, rows, ref totalCount);
- return new PagedResult
(rows, totalCount, page, result); - }
-
- //更新列
- public Task<int> UpdateLine(BookInfo entity, Expression
object >> columns, Expressionobject >> expression) - {
-
- return db.Updateable
(entity).UpdateColumns(columns).WhereColumns(expression).ExecuteCommandAsync(); -
- }
-
-
-
- }
- }