Пример #1
0
        /// <summary>
        /// 根据Id去删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool DeleteById <T>(int Id)
        {
            string sql = SqlBuider <T> .GetSql(dataType.DeleteModel);

            MySqlParameter[] parList = new MySqlParameter[] { new MySqlParameter("@Id", Id) };
            return(Excet <bool>(sql, parList, cmd => cmd.ExecuteNonQuery() == 1));
        }
Пример #2
0
        /// <summary>
        /// 根据条件去进行查询
        ///  ConditionBuilderVisitor2
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="expression">条件表达式</param>
        /// <returns>单个实体</returns>
        //public T GetModel<T>(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;
        //    });
        //    return default(T);
        //}

        /// <summary>
        /// 自写表达式树 根据条件去查询实体
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="expression">表达式</param>
        /// <returns>单个实体</returns>
        public T GetModel <T>(Expression <Func <T, bool> > expression)
        {
            QinVisitor <T> visitor = new QinVisitor <T>(expression);

            (string res, Dictionary <string, object> pairs) = visitor.GetResult();
            List <MySqlParameter> pp = pairs.GetMySqlPar();
            string sql = SqlBuider <T> .GetSql(dataType.QCondition) + res;

            return(Excet <T>(sql, pp.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]);
                    }
                }
                else
                {
                    return default;
                }
                return t;
            }));
        }
Пример #3
0
        /// <summary>
        /// 根据条件去进行查询
        ///  ConditionBuilderVisitor2
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        public List <T> GetList <T>(Expression <Func <T, bool> > expression)
        {
            // ConditionBuilderVisitor2 zlc = new ConditionBuilderVisitor2(expression);
            // var res = zlc.Condition();
            QinVisitor <T> visitor = new QinVisitor <T>(expression);

            (string res, Dictionary <string, object> pairs) = visitor.GetResult();
            List <MySqlParameter> pp = pairs.GetMySqlPar();

            string sql = SqlBuider <T> .GetSql(dataType.QCondition) + res;

            return(Excet(sql, pp.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().GetPropertiesName(false))
                    {
                        prop.SetValue(t, read[prop.Name] is DBNull ? null : read[prop.Name]);
                    }
                    list.Add(t);
                    t = Activator.CreateInstance <T>();//重置新的对象
                }
                return list;
            }));
        }
Пример #4
0
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Add <T>(T t)
        {
            Type   type = typeof(T);
            string sql  = SqlBuider <T> .GetSql(dataType.Insert);

            MySqlParameter[] parList = type.GetPropertiesName().Select(w => new MySqlParameter($"@{w.Name}", w.GetValue(t) ?? DBNull.Value)).ToArray();
            return(Excet <bool>(sql, parList, cmd => cmd.ExecuteNonQuery() == 1));
        }
Пример #5
0
        /// <summary>
        /// 查询全部【单表查询】
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List <T> GetList <T>()
        {
            string sql = SqlBuider <T> .GetSql(dataType.Query);

            return(Excet(sql, null, 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;
            }));
        }
Пример #6
0
        /// <summary>
        /// 根据Id查询单个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public T GetModelById <T>(int Id)
        {
            T      t   = Activator.CreateInstance <T>();
            string sql = SqlBuider <T> .GetSql(dataType.Query);

            MySqlParameter[] parList = new MySqlParameter[]
            {
                new MySqlParameter("@Id", Id)
            };
            return(Excet <T>(sql, parList, cmd =>
            {
                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;
            }));
        }