可以直接用,轻量方便,无需重复造轮子。 这里只对CRUD进行封装,我的应用比较简单。
csharp
using SqlSugar;
using System.Collections.Generic;
namespace MusicApp.Assist
{
internal class SqlSugarAssist
{
private static readonly object _lock = new object();
private static SqlSugarAssist _instance;
private readonly ISqlSugarClient _db;
private SqlSugarAssist(string connectionString)
{
_db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = connectionString,
DbType = DbType.SqlServer, // 或其他数据库类型
IsAutoCloseConnection = true
});
}
///单例实现
public static SqlSugarAssist GetInstance(string connectionString)
{
if (_instance == null)
{
lock (_lock) {
if (_instance == null)
{
_instance=new SqlSugarAssist(connectionString);
}
}
}
return _instance;
}
// 插入操作
public int Insert<T>(T entity) where T : class,new()
{
return _db.Insertable(entity).ExecuteCommand();
}
// 删除操作
public int Delete<T>(dynamic list) where T : class, new()
{
return _db.Deleteable<T>(list).ExecuteCommand();
}
// 更新操作
public int Update<T>(T entity) where T : class, new()
{
return _db.Updateable(entity).ExecuteCommand();
}
// 查询操作
public List<T> Select<T>()
{
return _db.Queryable<T>().ToList();
}
}
}