Exemplo n.º 1
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件的查询操作,返回对象DataTable(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="exp_Where"></param>
        /// <param name="exps_Sel"></param>
        /// <returns></returns>
        public DataTable SelectDataTable <T>(Expression <Func <T, bool> >[] exp_Where, params Expression <Func <T, object> >[] exps_Sel) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)));

            if (exps_Sel == null || exps_Sel.Count() <= 0)
            {
                sqlBuilder = sqlBuilder.Select();
            }
            else
            {
                sqlBuilder = sqlBuilder.Select(exps_Sel);
            }
            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();

            return(ExecuteDataTable(sql));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 以指定倒序排序字段查询数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp_Order"></param>
        /// <param name="exp_Where"></param>
        /// <returns></returns>
        public List <T> SelectOrderByDescending <T>(Expression <Func <T, object> > exp_Order, params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)));

            sqlBuilder = sqlBuilder.Select();
            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            if (exp_Order != null)
            {
                sqlBuilder = sqlBuilder.OrderBy(exp_Order, SortType.Desc);
            }

            string sql = sqlBuilder.GetSql();

            using (IDataReader dr = ExecuteDataReader(sql))
            {
                return(GetObjectList <T>(dr));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件检查是否存在(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="exp_Where"></param>
        /// <returns></returns>
        public bool Exists <T>(params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)))
                                         .Select("1");

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();
            object ret = ExecuteScalar(sql);

            if (ret != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件删除操作(条件部分只能进行And合并)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public ResultInfo <int> Delete <T>(params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)))
                                         .Delete();

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();
            int    ret = ExecuteNonQuery(sql);

            if (ret > 0)
            {
                return(new ResultInfo <int>(false, "", "0", int.Parse(ret.ToString())));
            }
            else
            {
                return(new ResultInfo <int>(true, "删除记录失败,删除记录数为0", GetErrCode("100001"), -1));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 针对符合要求的Model对象进行Update操作(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="exp_Where"></param>
        /// <param name="exp_Sel"></param>
        /// <returns></returns>
        public ResultInfo <int> Update <T>(T info, Expression <Func <T, bool> >[] exp_Where, params Expression <Func <T, object> >[] exp_Sel) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(info, TableModelMapper.GetTableName(typeof(T)))
                                         .Update()
                                         .Set(exp_Sel);

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();

            int ret = ExecuteNonQuery(sql);

            if (ret > 0)
            {
                return(new ResultInfo <int>(false, "", "0", ret));
            }
            else
            {
                return(new ResultInfo <int>(true, "更新记录失败", GetErrCode("100001"), -1));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 针对符合要求的Model对象进行单字段条件的查询操作,返回INT(条件部分只能进行And合并)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public int Count <T>(params Expression <Func <T, bool> >[] exp_Where) where T : ModelBase, new()
        {
            ISqlBuilder <T> sqlBuilder = CreateSqlBuilder <T>(TableModelMapper.GetTableName(typeof(T)))
                                         .SelectCount();

            if (exp_Where != null && exp_Where.Count() > 0)
            {
                sqlBuilder = sqlBuilder.Where(exp_Where[0]);
                if (exp_Where.Count() > 1)
                {
                    for (int i = 1; i < exp_Where.Count(); i++)
                    {
                        sqlBuilder = sqlBuilder.And(exp_Where[i]);
                    }
                }
            }
            string sql = sqlBuilder.GetSql();

            return(int.Parse(ExecuteScalar(sql).ToString()));
        }
Exemplo n.º 7
0
 public void TestAnd_1Condition()
 {
     _builder.And("a=1");
     Assert.AreEqual("a=1", _builder.GetPredicate());
 }