Example #1
0
        /// <summary>
        /// 根据实体创建动态参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t">实体</param>
        /// <returns></returns>
        protected DynamicParameters CreateDynamicParameters <T>(T t)
        {
            DynamicParameters res = new DynamicParameters();

            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                if (EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperIgnoreAttribute)))
                {
                    continue;
                }

                //if (pi.PropertyType == typeof(DateTime))
                //{
                //	//7.0 时间可为空
                //	if (Convert.ToDateTime(pi.GetValue(t, null)).Year < 2000)
                //		res.Add(pi.Name, null);
                //	else
                //		res.Add(pi.Name, pi.GetValue(t, null));
                //}
                //else
                res.Add(pi.Name, pi.GetValue(t, null));
            }

            return(res);
        }
Example #2
0
        /// <summary>
        /// 根据实体创建动态参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t">实体</param>
        /// <returns></returns>
        protected DynamicParameters CreateDynamicParameters <T>(T t)
        {
            DynamicParameters res = new DynamicParameters();

            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                if (EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperIgnoreAttribute)))
                {
                    continue;
                }

                if (pi.PropertyType == typeof(DateTime))
                {
                    if (Convert.ToDateTime(pi.GetValue(t, null)).Year < 2000)
                    {
                        res.Add(pi.Name, null);
                    }
                    else
                    {
                        //格式化微秒 防止6.0平台错误,同时7.0时间可为空
                        res.Add(pi.Name, DateTime.Parse(Convert.ToDateTime(pi.GetValue(t, null)).ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                }
                else
                {
                    res.Add(pi.Name, pi.GetValue(t, null));
                }
            }

            return(res);
        }
Example #3
0
        /// <summary>
        /// 生成 UPDATE 语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual string Update <T>() where T : class
        {
            StringBuilder strbRes1 = new StringBuilder("UPDATE " + EntityReflectionUtil.GetTableName <T>() + " SET ");

            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                if (EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperPrimaryKeyAttribute)) || EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperIgnoreAttribute)) || EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperPrimaryKeyAddAttribute)))
                {
                    continue;
                }
                strbRes1.AppendFormat("{0}={1}{0},", pi.Name, this.paramPrefix);
            }

            return(string.Format("{0} WHERE {1}={2}{1} ", strbRes1.ToString().TrimEnd(','), EntityReflectionUtil.GetPrimaryKey <T>(), this.paramPrefix));
        }
Example #4
0
        /// <summary>
        /// 检查实体DateTime的合法性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public void CheckEntityDateTime <T>(T t) where T : class
        {
            foreach (PropertyInfo pi in typeof(T).GetProperties().Where(a => a.PropertyType == typeof(DateTime)))
            {
                if (EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperIgnoreAttribute)))
                {
                    continue;
                }

                DateTime dt = (DateTime)pi.GetValue(t, null);
                if (dt == DateTime.MinValue)
                {
                    pi.SetValue(t, System.Data.SqlTypes.SqlDateTime.MinValue.Value, null);
                }
            }
        }
Example #5
0
        /// <summary>
        /// 生成 INSERT 语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual string Insert <T>() where T : class
        {
            StringBuilder strbRes1 = new StringBuilder("INSERT INTO " + EntityReflectionUtil.GetTableName <T>() + "(");
            StringBuilder strbRes2 = new StringBuilder("VALUES(");

            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                if (EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperIgnoreAttribute)) || EntityReflectionUtil.HasPropertyInfoAttribute(pi, typeof(DapperPrimaryKeyAddAttribute)))
                {
                    continue;
                }

                strbRes1.AppendFormat("{0},", pi.Name);
                strbRes2.AppendFormat("{1}{0},", pi.Name, this.paramPrefix);
            }

            return(string.Format("{0}) {1})", strbRes1.ToString().TrimEnd(','), strbRes2.ToString().TrimEnd(',')));
        }