代码修改后的版本,全部提交
This commit is contained in:
98
Mtxfw.Utility/DataAccess/Base.cs
Normal file
98
Mtxfw.Utility/DataAccess/Base.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
public Sql()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 条件查询
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public SqlSugarClient Sugar()
|
||||
{
|
||||
return Db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 条件查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public ISugarQueryable<T> Queryable<T>() where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在记录
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public bool Exists<T>(Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Any(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最大值 (单个字段)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public int Max<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> maxField) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Max(maxField);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最小值 (单个字段)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public int Min<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> minField) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Min(minField);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询最小值 (单个字段)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public object Min<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> minField) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Min(minField);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询总和 (单个字段)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public decimal Sum<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal>> sumField) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Sum(sumField);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询平均值 (单个字段)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public decimal Avg<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal>> avgField) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Avg(avgField);
|
||||
}
|
||||
}
|
||||
}
|
||||
99
Mtxfw.Utility/DataAccess/Common.cs
Normal file
99
Mtxfw.Utility/DataAccess/Common.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
public partial class Sql :DbHelperSqlSugar
|
||||
{
|
||||
#region 分类迭代
|
||||
/// <summary>
|
||||
/// 迭代查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate">查询条件 linq语句</param>
|
||||
/// <param name="parent_id">上级ID</param>
|
||||
/// <param name="channel_id">频道ID</param>
|
||||
/// <param name="isLevel">是否添加等级字段 (class_layer)</param>
|
||||
/// <param name="orderBy">排序</param>
|
||||
/// <param name="parent_field">上级字段名称 (如:parent_id)</param>
|
||||
/// <param name="strWhere">其他条件</param>
|
||||
/// <returns></returns>
|
||||
public DataTable Iteration<T>(Expression<Func<T, bool>> predicate, int parent_id = 0, int channel_id = 0, bool isLevel = false, string orderBy = "id desc", string parent_field = "parent_id", string strWhere = "") where T : class, new()
|
||||
{
|
||||
DataTable dt = Db.Queryable<T>().Where(predicate)
|
||||
.WhereIF(channel_id > 0, "channel_id=" + channel_id)
|
||||
.Where(strWhere)
|
||||
.OrderBy(orderBy).ToDataTable();
|
||||
//复制结构
|
||||
DataTable newData = dt.Clone();
|
||||
if (isLevel)
|
||||
{
|
||||
newData.Columns.Add("class_list", typeof(string));
|
||||
newData.Columns.Add("class_layer", typeof(int));
|
||||
GetChildsLayer(dt, newData, parent_field, parent_id, "", 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
GetChilds(dt, newData, parent_field, parent_id, channel_id);
|
||||
}
|
||||
//调用迭代组合成DAGATABLE
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从内存中取得所有下级类别列表(自身迭代)
|
||||
/// </summary>
|
||||
public void GetChilds(DataTable oldData, DataTable newData, string parent_field, int parent_id, int channel_id)
|
||||
{
|
||||
DataRow[] dr = oldData.Select(parent_field + "=" + parent_id);
|
||||
for (int i = 0; i < dr.Length; i++)
|
||||
{
|
||||
DataRow row = newData.NewRow();//创建新行
|
||||
//循环查找列数量赋值
|
||||
for (int j = 0; j < dr[i].Table.Columns.Count; j++)
|
||||
{
|
||||
row[dr[i].Table.Columns[j].ColumnName] = dr[i][dr[i].Table.Columns[j].ColumnName];
|
||||
}
|
||||
newData.Rows.Add(row);
|
||||
//调用自身迭代
|
||||
this.GetChilds(oldData, newData, parent_field, int.Parse(dr[i]["id"].ToString()), channel_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从内存中取得所有下级类别列表(自身迭代)
|
||||
/// </summary>
|
||||
private void GetChildsLayer(DataTable oldData, DataTable newData, string parent_field, int parent_id, string str_ids, int class_layer)
|
||||
{
|
||||
class_layer++;
|
||||
DataRow[] dr = oldData.Select(parent_field + "=" + parent_id);
|
||||
for (int i = 0; i < dr.Length; i++)
|
||||
{
|
||||
DataRow row = newData.NewRow();//创建新行
|
||||
//循环查找列数量赋值
|
||||
string strId = ""; string strClassList = "";
|
||||
for (int j = 0; j < dr[i].Table.Columns.Count; j++)
|
||||
{
|
||||
row[dr[i].Table.Columns[j].ColumnName] = dr[i][dr[i].Table.Columns[j].ColumnName];
|
||||
if ("id".Equals(dr[i].Table.Columns[j].ColumnName))
|
||||
{
|
||||
strId = dr[i][dr[i].Table.Columns[j].ColumnName].ToString();
|
||||
}
|
||||
if ("class_list".Equals(dr[i].Table.Columns[j].ColumnName))
|
||||
{
|
||||
strClassList = dr[i][dr[i].Table.Columns[j].ColumnName].ToString();
|
||||
}
|
||||
}
|
||||
|
||||
row["class_list"] = strClassList + "," + strId;
|
||||
row["class_layer"] = class_layer;//赋值深度字段
|
||||
newData.Rows.Add(row);//添加新行
|
||||
//调用自身迭代
|
||||
this.GetChildsLayer(oldData, newData, parent_field, int.Parse(dr[i]["id"].ToString()), str_ids, class_layer);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
50
Mtxfw.Utility/DataAccess/DataSet.cs
Normal file
50
Mtxfw.Utility/DataAccess/DataSet.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// DataSet类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql -> select * from table where id=@id and name=@name</param>
|
||||
/// <param name="parameters">参数 -> new {id=1,name="a"}</param>
|
||||
/// <returns>返回DataSet数据</returns>
|
||||
public DataSet GetDataSet(string sql, object parameters)
|
||||
{
|
||||
return Db.Ado.GetDataSetAll(sql, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql -> select * from table where id=@id and name=@name</param>
|
||||
/// <param name="parameters">
|
||||
/// 参数 -> new SugarParameter("@id",1),
|
||||
/// 多个参数 -> new SugarParameter []{ new SugarParameter("@id",1), new SugarParameter("@name",2)}
|
||||
/// </param>
|
||||
/// <returns>返回DataSet数据</returns>
|
||||
public DataSet GetDataSet(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
return Db.Ado.GetDataSetAll(sql, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql -> select * from table where id=@id and name=@name</param>
|
||||
/// <param name="parameters">
|
||||
/// 多个参数 -> new List<SugarParameter>(){ new SugarParameter("@id",1), new SugarParameter("@name",2) }
|
||||
/// </param>
|
||||
/// <returns>返回DataSet数据</returns>
|
||||
public DataSet GetDataSet(string sql, List<SugarParameter> parameters)
|
||||
{
|
||||
return Db.Ado.GetDataSetAll(sql, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Mtxfw.Utility/DataAccess/DataTable.cs
Normal file
130
Mtxfw.Utility/DataAccess/DataTable.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq.Expressions;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// DataTable类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql -> select * from table where id=@id and name=@name</param>
|
||||
/// <param name="parameters">参数 -> new {id=1,name="a"}</param>
|
||||
/// <returns>返回DataSet数据</returns>
|
||||
public DataTable GetDataTable(string sql, object parameters)
|
||||
{
|
||||
return Db.Ado.GetDataTable(sql, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql -> select * from table where id=@id and name=@name</param>
|
||||
/// <param name="parameters">
|
||||
/// 参数 -> new SugarParameter("@id",1),
|
||||
/// 多个参数 -> new SugarParameter []{ new SugarParameter("@id",1), new SugarParameter("@name",2)}
|
||||
/// </param>
|
||||
/// <returns>返回DataSet数据</returns>
|
||||
public DataTable GetDataTable(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
return Db.Ado.GetDataTable(sql, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataSet
|
||||
/// </summary>
|
||||
/// <param name="sql">sql -> select * from table where id=@id and name=@name</param>
|
||||
/// <param name="parameters">
|
||||
/// 多个参数 -> new List<SugarParameter>(){ new SugarParameter("@id",1), new SugarParameter("@name",2) }
|
||||
/// </param>
|
||||
/// <returns>返回DataSet数据</returns>
|
||||
public DataTable GetDataTable(string sql, List<SugarParameter> parameters)
|
||||
{
|
||||
return Db.Ado.GetDataTable(sql, parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataTable(带分页)
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="page">当前页</param>
|
||||
/// <param name="pageSize">每页显示条数</param>
|
||||
/// <param name="orderBy">排序</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataTable(string sql, int page, int pageSize, string orderBy)
|
||||
{
|
||||
int recordCount = Db.Ado.GetInt(PagingHelper.CreateCountingSql(sql));
|
||||
return Db.Ado.GetDataTable(PagingHelper.CreatePagingSql(recordCount, pageSize, page, sql, orderBy));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过SQL语句获取DataTable(带分页)
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="page">当前页</param>
|
||||
/// <param name="pageSize">每页显示条数</param>
|
||||
/// <param name="orderBy">排序</param>
|
||||
/// <param name="recordCount">总记录数 返回值</param>
|
||||
public DataTable GetDataTable(string sql, int page, int pageSize, string orderBy, out int recordCount)
|
||||
{
|
||||
recordCount = Db.Ado.GetInt(PagingHelper.CreateCountingSql(sql));
|
||||
return Db.Ado.GetDataTable(PagingHelper.CreatePagingSql(recordCount, pageSize, page, sql, orderBy));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable FindToDataTable<T>(Expression<Func<T, bool>> predicate, string orderBy = "") where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToDataTable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable FindToDataTable<T>(string strWhere, string orderBy = "") where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(strWhere).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToDataTable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable FindToDataTableByWhere<T>(string strWhere = "", string orderBy = "") where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy).ToDataTable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderByPredicate"></param>
|
||||
/// <param name="orderType"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable FindToDataTablePage<T>(int pageIndex, int pageSize, Expression<Func<T, bool>> predicate, string orderBy, ref int records) where T : class, new()
|
||||
{
|
||||
int count = GetCount(predicate);
|
||||
var page = Db.Queryable<T>().Where(predicate).OrderBy(orderBy).ToDataTablePage(pageIndex, pageSize, ref records);
|
||||
return page;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Mtxfw.Utility/DataAccess/Delete.cs
Normal file
56
Mtxfw.Utility/DataAccess/Delete.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 删除类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据实体类删除数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <returns></returns>
|
||||
public int Del<T>(T entity) where T : class, new()
|
||||
{
|
||||
return Db.Deleteable<T>(entity).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键删除数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="keyValue">主键</param>
|
||||
/// <returns></returns>
|
||||
public int Del<T>(int keyValue) where T : class, new()
|
||||
{
|
||||
return Db.Deleteable<T>(keyValue).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键批量删除
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="keyValues">主键集合</param>
|
||||
/// <returns></returns>
|
||||
public int Del<T>(object[] keyValues) where T : class, new()
|
||||
{
|
||||
return Db.Deleteable<T>(keyValues).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件删除
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="predicate">linq 条件 -> (t => t.id == 1 && t.name == "aaaa")</param>
|
||||
/// <returns></returns>
|
||||
public int Del<T>(Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Deleteable<T>(predicate).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Mtxfw.Utility/DataAccess/Execute.cs
Normal file
21
Mtxfw.Utility/DataAccess/Execute.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
using SqlSugar;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
public int ExecuteSql(string sql)
|
||||
{
|
||||
return Db.Ado.ExecuteCommand(sql);
|
||||
}
|
||||
|
||||
public int ExecuteSql(string sql, SugarParameter[] parameters)
|
||||
{
|
||||
return Db.Ado.ExecuteCommand(sql, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
105
Mtxfw.Utility/DataAccess/Insert.cs
Normal file
105
Mtxfw.Utility/DataAccess/Insert.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 插入类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 插入一条记录
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <returns>插入并返回自增列</returns>
|
||||
public int Insert<T>(T entity) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(entity).ExecuteReturnIdentity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entitys">实体类集合</param>
|
||||
/// <returns>插入并返回受影响行数</returns>
|
||||
public int Insert<T>(List<T> entitys) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(entitys.ToArray()).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="dt">实体类集合</param>
|
||||
/// <returns>插入并返回受影响行数</returns>
|
||||
public int Insert<T>(Dictionary<string, object> dt) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(dt).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只插入指定列数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="predicate">插入的指定列 -> it => new { it.Name, it.SchoolId }, 只插入列 Name和SchoolId</param>
|
||||
/// <returns></returns>
|
||||
public int InsertColumns<T>(T entity, Expression<Func<T, object>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(entity).InsertColumns(predicate).ExecuteReturnIdentity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只插入指定列数据(批量插入)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entity">实体类集合</param>
|
||||
/// <param name="predicate">插入的指定列 -> it => new { it.Name, it.SchoolId }, 只插入列 Name和SchoolId</param>
|
||||
/// <returns></returns>
|
||||
public int InsertColumns<T>(List<T> entitys, Expression<Func<T, object>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(entitys.ToArray()).InsertColumns(predicate).ExecuteReturnIdentity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不插入指定列数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="predicate">插入的指定列 -> it => new { it.Name, it.SchoolId }, 只插入列 Name和SchoolId</param>
|
||||
/// <returns></returns>
|
||||
public int InsertIgnoreColumns<T>(T entity, Expression<Func<T, object>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(entity).IgnoreColumns(predicate).ExecuteReturnIdentity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不插入指定列数据(批量插入)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名称</typeparam>
|
||||
/// <param name="entity">实体类集合</param>
|
||||
/// <param name="predicate">
|
||||
/// 插入的指定列 -> it => new { it.Name, it.SchoolId }, 只插入列 Name和SchoolId
|
||||
/// 或者 it => it == "Name" || it == "TestId"
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
public int InsertIgnoreColumns<T>(List<T> entitys, Expression<Func<T, object>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Insertable(entitys.ToArray()).IgnoreColumns(predicate).ExecuteReturnIdentity();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入一列数据
|
||||
/// </summary>
|
||||
public int InsertField<T>(Dictionary<string, object> dic) where T : class, new()
|
||||
{
|
||||
return Db.Insertable<T>(dic).ExecuteReturnIdentity();
|
||||
}
|
||||
}
|
||||
}
|
||||
188
Mtxfw.Utility/DataAccess/Select.cs
Normal file
188
Mtxfw.Utility/DataAccess/Select.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询所有
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public List<T> FindAll<T>() where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 条件查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderByPredicate"></param>
|
||||
/// <param name="orderType"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> FindAll<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByPredicate, SqlSugar.OrderByType orderType) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderByPredicate, orderType).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 条件查询,带排序
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderByStr"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> FindAll<T>(Expression<Func<T, bool>> predicate, string orderByStr) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderByStr).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据主键查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public T Find<T>(int keyValue) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().InSingle(keyValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键查询
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public T Find<T>(long keyValue) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().InSingle(keyValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询第一条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public T FindFirst<T>(Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).First();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询第一条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public T FindFirst<T>(Expression<Func<T, bool>> predicate, string orderBy) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderBy).First();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> Find<T>(Expression<Func<T, bool>> predicate, string orderBy) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderBy).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取记录条数
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public int GetCount<T>(Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Count();
|
||||
}
|
||||
/// <summary>
|
||||
/// 取前几条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="count"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderByPredicate"></param>
|
||||
/// <param name="orderType"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> FindTake<T>(int count, Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByPredicate, SqlSugar.OrderByType orderType) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderByPredicate, orderType).Take(count).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取前几条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="count"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBy"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> FindTake<T>(int count, Expression<Func<T, bool>> predicate, string orderBy) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderBy).Take(count).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取前几条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="count"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> FindTake<T>(int count, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).Take(count).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行事务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sqlStr"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> FindTran<T>(Expression<Func<T, bool>> predicate, string orderBy) where T : class, new()
|
||||
{
|
||||
var result = Db.Ado.UseTran<List<T>>(() =>
|
||||
{
|
||||
return Db.Queryable<T>().Where(predicate).OrderBy(orderBy).ToList();
|
||||
throw new Exception("error haha");
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="T2"></typeparam>
|
||||
/// <param name="joinExpression"></param>
|
||||
/// <returns></returns>
|
||||
public void FindTest<T, T2>(Expression<Func<T, T2, object[]>> joinExpression) where T : class, new()
|
||||
{
|
||||
Db.Queryable<T, T2>(joinExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
85
Mtxfw.Utility/DataAccess/Sql.cs
Normal file
85
Mtxfw.Utility/DataAccess/Sql.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行SQL或者存储过程类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行Sql语句和存储过程
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sqlStr"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public T SqlQuery<T>(string sqlStr, object obj) where T : class, new()
|
||||
{
|
||||
return Db.Ado.SqlQuery<T>(sqlStr, obj).SingleOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql语句返回List
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sqlStr"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> SqlQueryList<T>(string sqlStr, object obj) where T : class, new()
|
||||
{
|
||||
return Db.Ado.SqlQuery<T>(sqlStr, obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行事务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sqlStr"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> SqlQueryTran<T>(string sqlStr, object obj) where T : class, new()
|
||||
{
|
||||
var result = Db.Ado.UseTran<List<T>>(() =>
|
||||
{
|
||||
return Db.Ado.SqlQuery<T>(sqlStr, obj);
|
||||
throw new Exception("error haha");
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行事务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sqlStr"></param>
|
||||
/// <returns></returns>
|
||||
public int SqlQueryTran(string sqlStr)
|
||||
{
|
||||
var result = Db.Ado.UseTran(() =>
|
||||
{
|
||||
return Db.Ado.ExecuteCommand(sqlStr);
|
||||
throw new Exception("error haha");
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行事务
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sqlStr"></param>
|
||||
/// <returns></returns>
|
||||
public int SqlQueryTran(string sqlStr, object param)
|
||||
{
|
||||
var result = Db.Ado.UseTran(() =>
|
||||
{
|
||||
return Db.Ado.ExecuteCommand(sqlStr, param);
|
||||
throw new Exception("error haha");
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
132
Mtxfw.Utility/DataAccess/Update.cs
Normal file
132
Mtxfw.Utility/DataAccess/Update.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Mtxfw.Utility.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 修改类
|
||||
/// </summary>
|
||||
public partial class Sql : DbHelperSqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据实体更新(主键要有值,主键是更新条件)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <returns></returns>
|
||||
public int Update<T>(T entity) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(entity).ExecuteCommand();
|
||||
}
|
||||
/// <summary>
|
||||
/// 批量修改
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名</typeparam>
|
||||
/// <param name="entitys">实体类集合</param>
|
||||
/// <param name="predicate">修改条件 -> t => t.id == 1 && t.name == "aaaa"</param>
|
||||
/// <returns></returns>
|
||||
public int Update<T>(List<T> entitys, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(entitys.ToArray()).Where(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据表达示中的列更新,指定列并赋值的更新,比较常用
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类名</typeparam>
|
||||
/// <param name="columns">表达式列 -> it => new Student() { Name = "a", CreateTime = DateTime.Now }</param>
|
||||
/// <param name="predicate">修改条件 -> it => it.Id == 11</param>
|
||||
/// <returns></returns>
|
||||
public int Update<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>().UpdateColumns(columns).Where(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 4.2.3添加了WhereColumns 虽然XId不是主键但是 XId作为更新条件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体名</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="predicate">指定字段 -> it=>new{it.XId}</param>
|
||||
/// <returns></returns>
|
||||
public int UpdateWhereColumn<T>(T entity, Expression<Func<T, object>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(entity).WhereColumns(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 指定字段 以外的所有列 (主键要有值,主键是更新条件)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体名</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="predicate">
|
||||
/// 指定字段 -> it => new { it.Name, it.TestId }
|
||||
/// 或者 it => it=="name" 更新NAME 以外的所有列
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
public int UpdateIgnoreColumns<T>(T entity, Expression<Func<T, object>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(entity).IgnoreColumns(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实体更新,并且给指定列重新赋值 ,其它列也插入
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体名</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="predicate">it => it.Name == (it.Name + 1)</param>
|
||||
/// <returns></returns>
|
||||
public int UpdateReSetValue<T>(T entity, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(entity).ReSetValue(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新,并且给指定列重新赋值 ,其它列也插入
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体名</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="predicate">it => it.Name == (it.Name + 1)</param>
|
||||
/// <returns></returns>
|
||||
public int UpdateReSetValue<T>(List<T> list, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable(list).ReSetValue(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只更新指定字段并且 (entity不为空的情况)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体名</typeparam>
|
||||
/// <param name="entity">实体类</param>
|
||||
/// <param name="columns">修改字段 -> it=>new {it.Name}</param>
|
||||
/// <param name="predicate">修改的值 -> it => it.Name == (it.Name + 1)</param>
|
||||
/// <returns></returns>
|
||||
public int UpdateColumnsReSetValue<T>(T entity, Expression<Func<T, object>> columns, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(entity).UpdateColumns(columns).ReSetValue(predicate).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只更新指定字段并且 (entity为空的情况)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体名</typeparam>
|
||||
/// <param name="columns">修改字段 -> it => new Student() { Name = it.Name+1}</param>
|
||||
/// <param name="predicate">条件 -> it => it.Id == 11</param>
|
||||
/// <returns></returns>
|
||||
public int UpdateColumnsReSetValueToWhere<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
#pragma warning disable 618
|
||||
return Db.Updateable<T>().UpdateColumns(columns).Where(predicate).ExecuteCommand();
|
||||
#pragma warning restore 618
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改一列数据
|
||||
/// </summary>
|
||||
public bool UpdateField<T>(Dictionary<string, object> dic) where T : class, new()
|
||||
{
|
||||
return Db.Updateable<T>(dic).ExecuteCommand() > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user