|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200 |
- using Dapper;
- using Learun.Util;
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.Common;
- using System.Data.Entity;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
-
- namespace Learun.DataBase.MySqlEx
- {
- /// <summary>
- /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创建人:陈彬彬(Learun智慧校园数据库小组)
- /// 日 期:2017.03.04
- /// 描 述:数据库操作类
- /// </summary>
- public class Database : IDatabase
- {
- #region 构造函数
- /// <summary>
- /// 构造方法
- /// </summary>
- /// <param name="connString">连接串</param>
- public Database(string connString)
- {
- var obj = ConfigurationManager.ConnectionStrings[connString];
- string connectionString = obj == null ? connString : obj.ConnectionString;
- dbcontext = new DatabaseContext(connectionString);
- }
- #endregion
-
- #region 属性
- /// <summary>
- /// 获取 当前使用的数据访问上下文对象
- /// </summary>
- public DbContext dbcontext { get; set; }
- /// <summary>
- /// 事务对象
- /// </summary>
- public DbTransaction dbTransaction { get; set; }
- /// <summary>
- /// 获取连接上下文
- /// </summary>
- /// <returns></returns>
- public DbConnection getDbConnection()
- {
- return dbcontext.Database.Connection;
- }
- #endregion
-
- #region 事物提交
- /// <summary>
- /// 事务开始
- /// </summary>
- /// <returns></returns>
- public IDatabase BeginTrans()
- {
- //DbConnection dbConnection = ((IObjectContextAdapter)dbcontext).ObjectContext.Connection;
- //if (dbConnection.State == ConnectionState.Closed)
- //{
- // dbConnection.Open();
- //}
- //dbTransaction = dbConnection.BeginTransaction();
- //return this;
- if (dbcontext.Database.Connection.State == ConnectionState.Closed)
- {
- dbcontext.Database.Connection.Open();
- }
- dbTransaction = dbcontext.Database.Connection.BeginTransaction();
- dbcontext.Database.UseTransaction(dbTransaction);
- return this;
- }
-
- public async Task<int> CommitAsync()
- {
- try
- {
- int returnValue = await dbcontext.SaveChangesAsync();
- if (dbTransaction != null)
- {
- dbTransaction.Commit();
- this.Close();
- }
- return returnValue;
- }
- catch (Exception ex)
- {
- if (ex.InnerException != null && ex.InnerException.InnerException is MySqlException)
- {
- MySqlException sqlEx = ex.InnerException.InnerException as MySqlException;
- throw ExceptionEx.ThrowDataAccessException(sqlEx, sqlEx.Message);
- }
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 提交当前操作的结果
- /// </summary>
- public int Commit()
- {
- try
- {
- int returnValue = dbcontext.SaveChanges();
- if (dbTransaction != null)
- {
- dbTransaction.Commit();
- this.Close();
- }
- return returnValue;
- }
- catch (Exception ex)
- {
- if (ex.InnerException != null && ex.InnerException.InnerException is MySqlException)
- {
- MySqlException sqlEx = ex.InnerException.InnerException as MySqlException;
- throw ExceptionEx.ThrowDataAccessException(sqlEx, sqlEx.Message);
- }
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 把当前操作回滚成未提交状态
- /// </summary>
- public void Rollback()
- {
- this.dbTransaction.Rollback();
- this.dbTransaction.Dispose();
- this.Close();
- }
- /// <summary>
- /// 关闭连接 内存回收
- /// </summary>
- public void Close()
- {
- dbcontext.Dispose();
- }
- #endregion
-
- #region 执行 SQL 语句
- /// <summary>
- /// 执行sql语句
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <returns></returns>
- public int ExecuteBySql(string strSql)
- {
- try
- {
- return dbcontext.Database.Connection.Execute(strSql, null, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
-
- public async Task<int> ExecuteAsyncBySql(string strSql)
- {
- try
- {
- return await dbcontext.Database.Connection.ExecuteAsync(strSql, null, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 执行sql语句(带参数)
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public int ExecuteBySql(string strSql, object dbParameter)
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- return dbcontext.Database.Connection.Execute(strSql, dbParameter, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
-
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="procName">存储过程名称</param>
- /// <returns></returns>
- public int ExecuteByProc(string procName)
- {
- try
- {
- return dbcontext.Database.Connection.Execute(procName, null, dbTransaction, null, CommandType.StoredProcedure);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="procName">存储过程名称</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public int ExecuteByProc(string procName, object dbParameter)
- {
- try
- {
- return dbcontext.Database.Connection.Execute(procName, dbParameter, dbTransaction, null, CommandType.StoredProcedure);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
-
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="procName">存储过程名称</param>
- /// <returns></returns>
- public T ExecuteByProc<T>(string procName) where T : class
- {
- try
- {
- return dbcontext.Database.Connection.ExecuteScalar<T>(procName, null, dbTransaction, null, CommandType.StoredProcedure);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="procName">存储过程名称</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public T ExecuteByProc<T>(string procName, object dbParameter) where T : class
- {
- try
- {
- return dbcontext.Database.Connection.ExecuteScalar<T>(procName, dbParameter, dbTransaction, null, CommandType.StoredProcedure);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="procName">存储过程名称</param>
- /// <returns></returns>
- public IEnumerable<T> QueryByProc<T>(string procName) where T : class
- {
- try
- {
- return dbcontext.Database.Connection.Query<T>(procName, null, dbTransaction, true, null, CommandType.StoredProcedure);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="procName">存储过程名称</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public IEnumerable<T> QueryByProc<T>(string procName, object dbParameter) where T : class
- {
- try
- {
- return dbcontext.Database.Connection.Query<T>(procName, dbParameter, dbTransaction, true, null, CommandType.StoredProcedure);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- #endregion
-
- #region 对象实体 添加、修改、删除
- /// <summary>
- /// 插入实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entity">实体数据</param>
- /// <returns></returns>
- public int Insert<T>(T entity) where T : class
- {
- dbcontext.Entry<T>(entity).State = EntityState.Added;
- return dbTransaction == null ? this.Commit() : 0;
- }
-
- public async Task<int> InsertAsync<T>(IEnumerable<T> entities) where T : class
- {
- foreach (var entity in entities)
- {
- dbcontext.Entry<T>(entity).State = EntityState.Added;
- }
- return dbTransaction == null ? await this.CommitAsync() : 0;
- }
- /// <summary>
- /// 批量插入实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entities">实体数据列表</param>
- /// <returns></returns>
- public int Insert<T>(IEnumerable<T> entities) where T : class
- {
- foreach (var entity in entities)
- {
- dbcontext.Entry<T>(entity).State = EntityState.Added;
- }
- return dbTransaction == null ? this.Commit() : 0;
- }
- /// <summary>
- /// 删除实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entity">实体数据(需要主键赋值)</param>
- /// <returns></returns>
- public int Delete<T>(T entity) where T : class
- {
- dbcontext.Set<T>().Attach(entity);
- dbcontext.Set<T>().Remove(entity);
- return dbTransaction == null ? this.Commit() : 0;
- }
- /// <summary>
- /// 批量删除实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entities">实体数据列表</param>
- /// <returns></returns>
- public int Delete<T>(IEnumerable<T> entities) where T : class
- {
- foreach (var entity in entities)
- {
- dbcontext.Set<T>().Attach(entity);
- dbcontext.Set<T>().Remove(entity);
- }
- return dbTransaction == null ? this.Commit() : 0;
- }
- /// <summary>
- /// 删除表数据(根据Lambda表达式)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="condition"></param>
- /// <returns></returns>
- public int Delete<T>(Expression<Func<T, bool>> condition) where T : class,new()
- {
- IEnumerable<T> entities = dbcontext.Set<T>().Where(condition).ToList();
- return entities.Count() > 0 ? Delete(entities) : 0;
- }
- /// <summary>
- /// 更新实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entity">实体数据</param>
- /// <returns></returns>
- public int Update<T>(T entity) where T : class
- {
- this.UpdateEntity(entity);
- return dbTransaction == null ? this.Commit() : 0;
- }
- /// <summary>
- /// 更新实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entity">实体数据</param>
- /// <returns></returns>
- public int UpdateEx<T>(T entity) where T : class
- {
- dbcontext.Set<T>().Attach(entity);
- dbcontext.Entry(entity).State = EntityState.Modified;
- return dbTransaction == null ? this.Commit() : 0;
- }
- /// <summary>
- /// 批量更新实体数据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entities">实体数据列表</param>
- /// <returns></returns>
- public int Update<T>(IEnumerable<T> entities) where T : class
- {
- foreach (var entity in entities)
- {
- this.UpdateEntity(entity);
- }
- return dbTransaction == null ? this.Commit() : 0;
- }
- /// <summary>
- /// EF更新实体
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="entity">实体数据</param>
- private void UpdateEntity<T>(T entity) where T : class
- {
- dbcontext.Set<T>().Attach(entity);
- Hashtable props = SqlHelper.GetPropertyInfo<T>(entity);
- foreach (string item in props.Keys)
- {
- object value = dbcontext.Entry(entity).Property(item).CurrentValue;
- if (value != null)
- {
- if (value.ToString() == " ")
- dbcontext.Entry(entity).Property(item).CurrentValue = null;
- dbcontext.Entry(entity).Property(item).IsModified = true;
- }
- }
- }
- #endregion
-
- #region 对象实体 查询
- /// <summary>
- /// 查找一个实体根据主键
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="KeyValue">主键</param>
- /// <returns></returns>
- public T FindEntity<T>(object keyValue) where T : class
- {
- try
- {
- return dbcontext.Set<T>().Find(keyValue);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查找一个实体(根据表达式)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="condition">表达式</param>
- /// <returns></returns>
- public T FindEntity<T>(Expression<Func<T, bool>> condition) where T : class,new()
- {
- try
- {
- return dbcontext.Set<T>().Where(condition).FirstOrDefault();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查找一个实体(根据sql)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public T FindEntity<T>(string strSql, object dbParameter = null) where T : class,new()
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- var data = dbcontext.Database.Connection.Query<T>(strSql, dbParameter, dbTransaction);
- return data.FirstOrDefault();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 获取IQueryable表达式
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <returns></returns>
- public IQueryable<T> IQueryable<T>() where T : class,new()
- {
- try
- {
- return dbcontext.Set<T>();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 获取IQueryable表达式(根据表达式)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="condition">表达式</param>
- /// <returns></returns>
- public IQueryable<T> IQueryable<T>(Expression<Func<T, bool>> condition) where T : class,new()
- {
- try
- {
- return dbcontext.Set<T>().Where(condition);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表(获取表所有数据)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>() where T : class,new()
- {
- try
- {
- return dbcontext.Set<T>().ToList();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
-
- public async Task<IEnumerable<T>> FindListAsync<T>() where T : class, new()
- {
- try
- {
- return await dbcontext.Set<T>().ToListAsync();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表(获取表所有数据)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="orderby">排序</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(Func<T, object> keySelector) where T : class,new()
- {
- try
- {
- return dbcontext.Set<T>().OrderBy(keySelector).ToList();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表根据表达式
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="condition">表达式</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(Expression<Func<T, bool>> condition) where T : class,new()
- {
- try
- {
- return dbcontext.Set<T>().Where(condition).ToList();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表根据sql语句
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="strSql">sql语句</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(string strSql) where T : class
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- return dbcontext.Database.Connection.Query<T>(strSql, null, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- public async Task<IEnumerable<T>> FindListAsync<T>(string strSql) where T : class
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- return await dbcontext.Database.Connection.QueryAsync<T>(strSql, null, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表根据sql语句(带参数)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(string strSql, object dbParameter) where T : class
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- return dbcontext.Database.Connection.Query<T>(strSql, dbParameter, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
-
- public async Task<IEnumerable<T>> FindListAsync<T>(string strSql, object dbParameter) where T : class
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- return await dbcontext.Database.Connection.QueryAsync<T>(strSql, dbParameter, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表(分页)
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="orderField">排序字段</param>
- /// <param name="isAsc">排序类型</param>
- /// <param name="pageSize">每页数据条数</param>
- /// <param name="pageIndex">页码</param>
- /// <param name="total">总共数据条数</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class,new()
- {
- try
- {
- string[] _order = orderField.Split(',');
- MethodCallExpression resultExp = null;
- var tempData = dbcontext.Set<T>().AsQueryable();
- foreach (string item in _order)
- {
- string _orderPart = item;
- _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
- string[] _orderArry = _orderPart.Split(' ');
- string _orderField = _orderArry[0];
- bool sort = isAsc;
- if (_orderArry.Length == 2)
- {
- isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
- }
- var parameter = Expression.Parameter(typeof(T), "t");
- var property = typeof(T).GetProperty(_orderField);
- var propertyAccess = Expression.MakeMemberAccess(parameter, property);
- var orderByExp = Expression.Lambda(propertyAccess, parameter);
- resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
- }
- tempData = tempData.Provider.CreateQuery<T>(resultExp);
- total = tempData.Count();
- tempData = tempData.Skip<T>(pageSize * (pageIndex - 1)).Take<T>(pageSize).AsQueryable();
- return tempData.ToList();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 查询列表(分页)带表达式条件
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="condition">表达式</param>
- /// <param name="orderField">排序字段</param>
- /// <param name="isAsc">排序类型</param>
- /// <param name="pageSize">每页数据条数</param>
- /// <param name="pageIndex">页码</param>
- /// <param name="total">总共数据条数</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(Expression<Func<T, bool>> condition, string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class,new()
- {
- try
- {
- string[] _order = orderField.Split(',');
- MethodCallExpression resultExp = null;
- var tempData = dbcontext.Set<T>().Where(condition);
- foreach (string item in _order)
- {
- string _orderPart = item;
- _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
- string[] _orderArry = _orderPart.Split(' ');
- string _orderField = _orderArry[0];
- bool sort = isAsc;
- if (_orderArry.Length == 2)
- {
- isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
- }
- var parameter = Expression.Parameter(typeof(T), "t");
- var property = typeof(T).GetProperty(_orderField);
- var propertyAccess = Expression.MakeMemberAccess(parameter, property);
- var orderByExp = Expression.Lambda(propertyAccess, parameter);
- resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
- }
- tempData = tempData.Provider.CreateQuery<T>(resultExp);
- total = tempData.Count();
- tempData = tempData.Skip<T>(pageSize * (pageIndex - 1)).Take<T>(pageSize).AsQueryable();
- return tempData.ToList();
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
-
- }
- /// <summary>
- /// 查询列表(分页)根据sql语句
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strSql">sql语句</param>
- /// <param name="orderField">排序字段</param>
- /// <param name="isAsc">排序类型</param>
- /// <param name="pageSize">每页数据条数</param>
- /// <param name="pageIndex">页码</param>
- /// <param name="total">总共数据条数</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class
- {
- return FindList<T>(strSql, null, orderField, isAsc, pageSize, pageIndex, out total);
- }
- /// <summary>
- /// 查询列表(分页)根据sql语句
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <param name="orderField">排序字段</param>
- /// <param name="isAsc">排序类型</param>
- /// <param name="pageSize">每页数据条数</param>
- /// <param name="pageIndex">页码</param>
- /// <param name="total">总共数据条数</param>
- /// <returns></returns>
- public IEnumerable<T> FindList<T>(string strSql, object dbParameter, string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- StringBuilder sb = new StringBuilder();
- sb.Append(SqlHelper.MySqlPageSql(strSql, orderField, isAsc, pageSize, pageIndex));
- total = Convert.ToInt32(dbcontext.Database.Connection.ExecuteScalar("Select Count(1) From (" + strSql + ") As t", dbParameter));
- return dbcontext.Database.Connection.Query<T>(sb.ToString(), dbParameter, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
-
- }
- #endregion
-
- #region 数据源查询
- /// <summary>
- /// 查询数据
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <returns></returns>
- public DataTable FindTable(string strSql)
- {
- try
- {
- var IDataReader = dbcontext.Database.Connection.ExecuteReader(strSql, null, dbTransaction);
- return SqlHelper.IDataReaderToDataTable(IDataReader);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
-
- }
- /// <summary>
- /// 查询数据
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public DataTable FindTable(string strSql, object dbParameter)
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- var IDataReader = dbcontext.Database.Connection.ExecuteReader(strSql, dbParameter, dbTransaction);
- return SqlHelper.IDataReaderToDataTable(IDataReader);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
-
- }
- /// <summary>
- /// 查询数据
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <param name="orderField">排序字段</param>
- /// <param name="isAsc">排序类型</param>
- /// <param name="pageSize">每页数据条数</param>
- /// <param name="pageIndex">页码</param>
- /// <param name="total">总共数据条数</param>
- /// <returns></returns>
- public DataTable FindTable(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex, out int total)
- {
- return FindTable(strSql, null, orderField, isAsc, pageSize, pageIndex, out total);
- }
- /// <summary>
- /// 查询数据
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <param name="orderField">排序字段</param>
- /// <param name="isAsc">排序类型</param>
- /// <param name="pageSize">每页数据条数</param>
- /// <param name="pageIndex">页码</param>
- /// <param name="total">总共数据条数</param>
- /// <returns></returns>
- public DataTable FindTable(string strSql, object dbParameter, string orderField, bool isAsc, int pageSize, int pageIndex, out int total)
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- StringBuilder sb = new StringBuilder();
- sb.Append(SqlHelper.MySqlPageSql(strSql, orderField, isAsc, pageSize, pageIndex));
- total = Convert.ToInt32(dbcontext.Database.Connection.ExecuteScalar("Select Count(1) From (" + strSql + ") As t", dbParameter));
- var IDataReader = dbcontext.Database.Connection.ExecuteReader(sb.ToString(), dbParameter, dbTransaction);
- return SqlHelper.IDataReaderToDataTable(IDataReader);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
-
- }
- /// <summary>
- /// 获取查询对象
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <returns></returns>
- public object FindObject(string strSql)
- {
- return FindObject(strSql, null);
- }
- /// <summary>
- /// 获取查询对象
- /// </summary>
- /// <param name="strSql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public object FindObject(string strSql, object dbParameter)
- {
- try
- {
- strSql = strSql.Replace("@", "?");
- return dbcontext.Database.Connection.ExecuteScalar(strSql, dbParameter, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- #endregion
-
- #region 扩展方法
- /// <summary>
- /// 获取数据库表数据
- /// </summary>
- /// <typeparam name="T">反序列化类型</typeparam>
- /// <returns></returns>
- public IEnumerable<T> GetDBTable<T>() where T : class,new()
- {
- try
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append(@"
- SELECT t1.*,
- (select COLUMN_NAME from information_schema.`COLUMNS` where TABLE_SCHEMA=database() and TABLE_NAME =t1.name and COLUMN_KEY='PRI') pk
- from (
- SELECT TABLE_NAME name,0 reserved,0 data,0 index_size,TABLE_ROWS sumrows,0 unused, (select IF(LENGTH(TRIM(TABLE_COMMENT))<1,TABLE_NAME,TABLE_COMMENT)) tdescription
- from information_schema.`TABLES` where TABLE_SCHEMA =database()
- ) t1 ORDER BY t1.name
- ");
- return dbcontext.Database.Connection.Query<T>(strSql.ToString(), null, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- /// <summary>
- /// 获取数据库表字段数据
- /// </summary>
- /// <typeparam name="T">反序列化类型</typeparam>
- /// <param name="tableName">表名</param>
- /// <returns></returns>
- public IEnumerable<T> GetDBTableFields<T>(string tableName) where T : class,new()
- {
- try
- {
- StringBuilder strSql = new StringBuilder();
- strSql.Append(@" select ORDINAL_POSITION f_number, COLUMN_NAME f_column, DATA_TYPE f_datatype, if(CHARACTER_MAXIMUM_LENGTH is null,if(LOCATE('int',column_type)>0,REPLACE(REPLACE(column_type,'int(',''),')',''),0),CHARACTER_MAXIMUM_LENGTH) f_length, '' f_identity, IF(COLUMN_KEY='PRI','1','') f_key,
- IF(IS_NULLABLE='YES','1','') f_isnullable,COLUMN_DEFAULT f_default,case when COLUMN_COMMENT='' then COLUMN_NAME else COLUMN_COMMENT END f_remark from (
- select *
- from information_schema.`COLUMNS` t1 where TABLE_SCHEMA=database() and TABLE_NAME = ?tableName
- ) t2 order by f_number ");
- return dbcontext.Database.Connection.Query<T>(strSql.ToString(), new { tableName = tableName }, dbTransaction);
- }
- catch (Exception)
- {
-
- throw;
- }
- finally
- {
- if (dbTransaction == null)
- {
- this.Close();
- }
- }
- }
- #endregion
- }
- }
|