SqlSugar .Net ORM
var list = SqlSugarHelper.DemoDbContext.Query<ORMClsLib.dbo.DemoEntity>();
var item = new ORMClsLib.dbo.DemoEntity()
{
operatorName = "test",
};
SqlSugarHelper.DemoDbContext.InsertOrUpdate(item);
SqlSugarHelper.DemoDbContext.Delete<ORMClsLib.dbo.DemoEntity>(0);
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ORMClsLib
{
public class SqlSugarHelper
{
private static readonly DefaultParams _defaultParams = DefaultParams.Gethandler();
private static SqlSugarScope ServerDb = new SqlSugarScope(new ConnectionConfig()
{
ConnectionString = string.Format("server={0};database={1};uid=sa;pwd={2}", _defaultParams.IpSource, _defaultParams.DatabaseName, _defaultParams.Password),
DbType = DbType.SqlServer,
IsAutoCloseConnection = true
},
db =>
{
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
};
});
public static DbContext DemoDbContext = new DbContext(ServerDb);
}
public sealed class DbContext
{
private readonly SqlSugarScope _sqlSugarScope;
public DbContext(SqlSugarScope sqlSugarScope)
{
_sqlSugarScope = sqlSugarScope;
}
public List<T> Query<T>(Expression<Func<T, bool>> expression = null) where T : class
{
if (expression == null)
{
return _sqlSugarScope.Queryable<T>().ToList();
}
return _sqlSugarScope.Queryable<T>().Where(expression).ToList();
}
public T QueryMax<T>(string maxField)
{
return _sqlSugarScope.Queryable<T>().Max<T>(maxField);
}
public T QueryFirst<T>(Expression<Func<T, bool>> expression = null) where T : class
{
return _sqlSugarScope.Queryable<T>().First(expression);
}
public List<TResult> QueryDistinct<T, TResult>(Expression<Func<T, TResult>> expression)
{
return _sqlSugarScope.Queryable<T>().Select(expression).Distinct().ToList();
}
public List<T> QueryOrder<T>(Expression<Func<T, object>> expression = null) where T : class
{
return _sqlSugarScope.Queryable<T>().OrderBy(expression).ToList();
}
public List<T> QueryByDescending<T>(Expression<Func<T, object>> expression = null) where T : class
{
return _sqlSugarScope.Queryable<T>().OrderByDescending(expression).ToList();
}
#region Delete
public void Delete<T>(T item) where T : class, new()
{
_sqlSugarScope.Deleteable<T>(item).ExecuteCommand();
}
public void Delete<T>(IEnumerable<T> list) where T : class, new()
{
_sqlSugarScope.Deleteable<T>(list).ExecuteCommand();
}
public void Delete<T>(Expression<Func<T, bool>> expression) where T : class, new()
{
_sqlSugarScope.Deleteable<T>().Where(expression).ExecuteCommand();
}
public void Delete<T>(params int[] ids) where T : class, new()
{
_sqlSugarScope.Deleteable<T>().In(ids).ExecuteCommand();
}
#endregion
#region Add or Update
public bool InsertOrUpdate<T>(List<T> list) where T : class, new()
{
return _sqlSugarScope.Fastest<T>().BulkUpdate(list) == list.Count;
}
public bool InsertOrUpdate<T>(T item) where T : class, new()
{
var x = _sqlSugarScope.Storageable(item).ToStorage();
int rslt = x.AsInsertable.ExecuteCommand();
int rslt2 = x.AsUpdateable.ExecuteCommand();
return rslt + rslt2 > 1;
}
#endregion
}
}
namespace ORMClsLib.dbo
{
[SugarTable("DemoEntity")]
public partial class DemoEntity
{
public DemoEntity()
{
}
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int id { get; set; }
[SugarColumn(ColumnName = "operator")]
public string operatorName { get; set; }
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库