Esempio n. 1
0
        /// <summary>
        /// 拼接 查询 SQL语句,自定义条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where">条件</param>
        /// <param name="allFieid">是否查询所有字段</param>
        /// <returns></returns>
        public static StringBuilder SelectSql <T>(string where, bool allFieid = false) where T : class, new()
        {
            //表名
            string table = EntityAttributeHelper.GetEntityTable <T>();

            PropertyInfo[] props     = EntityAttributeHelper.GetProperties(typeof(T));
            StringBuilder  sbColumns = new StringBuilder();

            if (allFieid)
            {
                sbColumns.Append(" * ");
            }
            else
            {
                foreach (PropertyInfo prop in props)
                {
                    //string propertytype = prop.PropertyType.ToString();
                    sbColumns.Append("[" + prop.Name + "],");
                }
                if (sbColumns.Length > 0)
                {
                    sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
                }
            }

            if (string.IsNullOrWhiteSpace(where))
            {
                where = " WHERE 1 = 1";
            }

            string strSql = "SELECT {0} FROM {1} {2}";

            strSql = string.Format(strSql, sbColumns.ToString(), table + " ", where);
            return(new StringBuilder(strSql));
        }
Esempio n. 2
0
        /// <summary>
        /// 拼接 查询 SQL语句
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <returns></returns>
        public static StringBuilder SelectSql <T>(int top) where T : new()
        {
            //表名
            string table = EntityAttributeHelper.GetEntityTable <T>();

            PropertyInfo[] props     = EntityAttributeHelper.GetProperties(typeof(T));
            StringBuilder  sbColumns = new StringBuilder();

            foreach (PropertyInfo prop in props)
            {
                sbColumns.Append(prop.Name + ",");
            }
            if (sbColumns.Length > 0)
            {
                sbColumns.Remove(sbColumns.ToString().Length - 1, 1);
            }
            string strSql = "SELECT top {0} {1} FROM {2} WHERE 1=1 ";

            strSql = string.Format(strSql, top, sbColumns.ToString(), table + " ");
            return(new StringBuilder(strSql));
        }