Esempio n. 1
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 = string.Format("SELECT TOP {0} {1} FROM {2} WHERE 1=1 ", top, sbColumns.ToString(), table);

            return(new StringBuilder(strSql));
        }
Esempio n. 2
0
        /// <summary>
        /// 拼接 查询 SQL语句,自定义条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where">条件</param>
        /// <param name="allFieid">是否查询所有字段</param>
        /// <returns></returns>
        public static string SelectSql <T>(string where, bool allFieid = false) where T : class
        {
            string cacheKey = string.Format("T-SQL:SELECT:{0}:{1}", typeof(T).Name, string.Format("{0}{1}", typeof(T).FullName, allFieid.ToString()).GetMd5Code());
            string cache    = CacheFactory.GetCache().Get <string>(cacheKey);

            if (string.IsNullOrEmpty(cache))
            {
                //表名
                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 = string.Format("SELECT {0} FROM {1} {2}", sbColumns.ToString(), table, where);
                cache = strSql.ToString();
                CacheFactory.GetCache().Add(cacheKey, cache);
            }
            return(cache);
        }