Exemple #1
0
        /// <summary>
        /// 根据表达式的条件删除记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exception"></param>
        /// <returns></returns>
        public static bool Delete(Expression exception)
        {
            ConditionBuilderVisitor2 visitor = new ConditionBuilderVisitor2(exception);
            var    result = visitor.Condition();
            string sql    = SqlBuider <T> .GetSql(dataType.DeleteWhere) + result.Item1;

            return(Excet <bool>(sql, result.Item2.ToArray(), cmd => cmd.ExecuteNonQuery() == 1));
        }
Exemple #2
0
        /// <summary>
        /// 根据表达式条件去查找记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T GetModel(Expression <Func <T, bool> > expression)
        {
            ConditionBuilderVisitor2 visitor           = new ConditionBuilderVisitor2(expression);
            Tuple <string, List <MySqlParameter> > res = visitor.Condition();
            string sql = SqlBuider <User> .GetSql(dataType.QCondition) + res.Item1;

            return(Excet <T>(sql, res.Item2.ToArray(), cmd =>
            {
                T t = Activator.CreateInstance <T>();
                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    foreach (System.Reflection.PropertyInfo item in t.GetType().GetPropertiesName(false))
                    {
                        item.SetValue(t, reader[item.Name] is DBNull ? null : reader[item.Name]);
                    }
                }
                return t;
            }));
        }
Exemple #3
0
        /// <summary>
        /// 根据条件查询多条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List <T> GetList(Expression <Func <T, bool> > expression)
        {
            ConditionBuilderVisitor2 zlc = new ConditionBuilderVisitor2(expression);
            var    res = zlc.Condition();
            string sql = SqlBuider <T> .GetSql(dataType.QCondition) + res.Item1;

            return(Excet(sql, res.Item2.ToArray(), cmd =>
            {
                T t = Activator.CreateInstance <T>();
                List <T> list = new List <T>();
                var read = cmd.ExecuteReader();
                while (read.Read())
                {
                    foreach (System.Reflection.PropertyInfo prop in t.GetType().GetProperties())
                    {
                        prop.SetValue(t, read[prop.Name] is DBNull ? null : read[prop.Name]);
                    }
                    list.Add(t);
                    t = Activator.CreateInstance <T>();//重置新的对象
                }
                return list;
            }));
        }