Beispiel #1
0
        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="lsSql"></param>
        /// <param name="lsParas"></param>
        public void ExecuteTransaction(List <String> lsSql, List <Object[]> lsParas)
        {
            using (SysDb db = new SysDb(strConn))
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        for (int i = 0; i < lsSql.Count; i++)
                        {
                            if (lsParas != null && lsParas.Count > 0)
                            {
                                db.Database.ExecuteSqlCommand(lsSql[i], lsParas[i]);
                            }
                        }
                        foreach (String item in lsSql)
                        {
                            db.Database.ExecuteSqlCommand(item);
                        }

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw ex;
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// 查询列表
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <param name="paras"></param>
 /// <returns></returns>
 public List <T> QueryList <T>(string sql, params object[] paras) where T : class
 {
     using (SysDb db = new SysDb(strConn))
     {
         return(db.Database.SqlQuery <T>(sql, paras).ToList());
     }
 }
Beispiel #3
0
 /// <summary>
 /// 查询单个
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <param name="paras"></param>
 /// <returns></returns>
 public T QuerySingle <T>(string sql, params object[] paras) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(db.Database.SqlQuery <T>(sql, paras).FirstOrDefault());
     }
 }
Beispiel #4
0
 /// <summary>
 /// 获取所有实体集合
 /// </summary>
 /// <returns></returns>
 public List <T> GetAll <T>() where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(db.Set <T>().AsExpandable().ToList <T>());
     }
 }
Beispiel #5
0
 /// <summary>
 /// 执行操作
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="paras"></param>
 public int ExecuteSql(string sql, params object[] paras)
 {
     using (SysDb db = new SysDb(strConn))
     {
         return(db.Database.ExecuteSqlCommand(sql, paras));
     }
 }
Beispiel #6
0
 /// <summary>
 /// 根据条件查询实体集合(单个字段排序)
 /// </summary>
 /// <param name="seleWhere">查询条件 lambel表达式</param>
 /// <returns></returns>
 public List <T> GetList <T, Tkey>(Expression <Func <T, bool> > seleWhere, Expression <Func <T, Tkey> > orderWhere, bool isDesc) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(CommonSort(db.Set <T>().AsExpandable().Where(seleWhere), orderWhere, isDesc).ToList());
     }
 }
Beispiel #7
0
 /// <summary>
 /// 根据条件查询实体集合(多个字段排序)
 /// </summary>
 /// <param name="seleWhere">查询条件 lambel表达式</param>
 /// <returns></returns>
 public List <T> GetList <T>(Expression <Func <T, bool> > seleWhere, params OrderModelField[] orderByExpression) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(CommonSort(db.Set <T>().AsExpandable().Where(seleWhere), orderByExpression).ToList());
     }
 }
Beispiel #8
0
 /// <summary>
 /// 根据条件查询实体集合
 /// </summary>
 /// <param name="seleWhere">查询条件 lambel表达式</param>
 /// <returns></returns>
 public List <T> GetList <T, TValue>(Expression <Func <T, TValue> > seleWhere, IEnumerable <TValue> conditions) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(db.Set <T>().AsExpandable().WhereIn <T, TValue>(seleWhere, conditions).ToList());
     }
 }
Beispiel #9
0
 /// <summary>
 /// 根据条件查询实体集合
 /// </summary>
 /// <param name="seleWhere">查询条件 lambel表达式</param>
 /// <returns></returns>
 public List <T> GetList <T>(Expression <Func <T, bool> > seleWhere) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(db.Set <T>().AsExpandable().Where(seleWhere).ToList());
     }
 }
Beispiel #10
0
 /// <summary>
 /// 获取所有实体集合(多个排序)
 /// </summary>
 /// <returns></returns>
 public List <T> GetAll <T>(params OrderModelField[] orderByExpression) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(CommonSort(db.Set <T>().AsExpandable(), orderByExpression).ToList());
     }
 }
Beispiel #11
0
 /// <summary>
 /// 查找单个
 /// </summary>
 /// <param name="id">主键</param>
 /// <returns></returns>
 public T GetSingleById <T>(int id) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(db.Set <T>().Find(id));
     }
 }
Beispiel #12
0
 /// <summary>
 /// 查找单个
 /// </summary>
 /// <param name="seleWhere">查询条件</param>
 /// <returns></returns>
 public T GetSingle <T>(Expression <Func <T, bool> > seleWhere) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         return(db.Set <T>().AsExpandable().FirstOrDefault(seleWhere));
     }
 }
Beispiel #13
0
 /// <summary>
 /// 新增
 /// </summary>
 /// <param name="entity">实体</param>
 /// <returns>返回受影响行数</returns>
 public bool Add <T>(T entity) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         db.Entry <T>(entity).State = EntityState.Added;
         return(db.SaveChanges() > 0);
     }
 }
Beispiel #14
0
 /// <summary>
 /// 根据条件删除
 /// </summary>
 /// <param name="deleWhere">删除条件</param>
 /// <returns>返回受影响行数</returns>
 public bool DeleteByConditon <T>(Expression <Func <T, bool> > deleWhere) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         List <T> entitys = db.Set <T>().Where(deleWhere).ToList();
         entitys.ForEach(m => db.Entry <T>(m).State = EntityState.Deleted);
         return(db.SaveChanges() > 0);
     }
 }
Beispiel #15
0
 /// <summary>
 /// 删除
 /// </summary>
 /// <param name="entity">实体</param>
 /// <returns>返回受影响行数</returns>
 public bool Delete <T>(T entity) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         db.Set <T>().Attach(entity);
         db.Entry <T>(entity).State = EntityState.Deleted;
         return(db.SaveChanges() > 0);
     }
 }
Beispiel #16
0
 /// <summary>
 /// 获取分页集合(无条件多字段排序)
 /// </summary>
 /// <returns></returns>
 public List <T> GetListPaged <T>(int pageIndex, int pageSize, out int totalcount, params OrderModelField[] orderByExpression) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         totalcount = db.Set <T>().AsExpandable().Count();//获取总数
         //需要增加AsExpandable(),否则查询的是所有数据到内存,然后再排序  AsExpandable是linqkit.dll中的方法
         return(CommonSort(db.Set <T>().AsExpandable(), orderByExpression).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList());
     }
 }
Beispiel #17
0
 /// <summary>
 /// 获取分页集合(有条件无排序)
 /// </summary>
 /// <returns></returns>
 public List <T> GetListPaged <T, Tkey>(int pageIndex, int pageSize, Expression <Func <T, bool> > seleWhere, out int totalcount) where T : class
 {
     using (SysDb <T> db = new SysDb <T>(strConn))
     {
         totalcount = db.Set <T>().AsExpandable().Where(seleWhere).Count();//获取总数
         //需要增加AsExpandable(),否则查询的是所有数据到内存,然后再排序  AsExpandable是linqkit.dll中的方法
         return(db.Set <T>().AsExpandable().Where(seleWhere).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList());
     }
 }
Beispiel #18
0
 /// <summary>
 /// 添加日志
 /// </summary>
 /// <param name="info"></param>
 public void AddLogger(string info)
 {
     if (info != "\r\n" && (!info.Contains("Sys_EventLog")))
     {
         string strTemp = info.ToUpper().Trim();
         if (isNew)
         {
             //记录增删改
             if (strTemp.StartsWith("INSERT") || strTemp.StartsWith("UPDATE") || strTemp.StartsWith("DELETE"))
             {
                 strMsg = info;
                 isNew  = false;
             }
         }
         else
         {
             if (strTemp.StartsWith("CLOSED CONNECTION"))
             {
                 //增加新日志
                 using (SysDb db = new SysDb(strConn))
                 {
                     try
                     {
                         //保存日志到数据库或其他地方
                     }
                     catch (Exception ex)
                     {
                         using (System.IO.StreamWriter sw = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "//logError.txt"))
                         {
                             sw.Write(ex.Message);
                             sw.Flush();
                         }
                     }
                 }
                 //清空
                 strMsg = "";
                 isNew  = true;
             }
             else
             {
                 strMsg += info;
             }
         }
     }
 }