示例#1
0
        /// <summary>
        /// 拼接删除SQL语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public static StringBuilder DeleteSql <T>(T entity)
        {
            string tableName = DatabaseCommon.GetTableName <T>().ToString();
            Type   type      = entity.GetType();

            PropertyInfo[] props = type.GetProperties();
            StringBuilder  sb    = new StringBuilder("Delete From " + tableName + " Where 1=1");

            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    sb.Append(" AND " + prop.Name + " = " + DbHelper.DbParmChar + "" + prop.Name + "");
                }
            }
            return(sb);
        }
示例#2
0
        /// <summary>
        /// 拼接 查询 SQL语句
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <returns></returns>
        public static StringBuilder SelectSql <T>(int top) where T : new()
        {
            string tableName = DatabaseCommon.GetTableName <T>().ToString();

            PropertyInfo[] props     = GetProperties(new T().GetType());
            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(), tableName + " ");
            return(new StringBuilder(strSql));
        }
示例#3
0
        /// <summary>
        /// 泛型方法,反射生成UpdateSql语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>int</returns>
        public static StringBuilder UpdateSql <T>(T entity)
        {
            string tableName = DatabaseCommon.GetTableName <T>().ToString();

            string pkName = GetKeyField <T>().ToString();
            Type   type   = entity.GetType();

            PropertyInfo[] props = type.GetProperties();
            StringBuilder  sb    = new StringBuilder();

            sb.Append("Update ");
            sb.Append(tableName);
            sb.Append(" Set ");
            bool isFirstValue = true;

            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null && pkName != prop.Name)
                {
                    if (isFirstValue)
                    {
                        isFirstValue = false;
                        sb.Append(prop.Name);
                        sb.Append("=");
                        sb.Append(DbHelper.DbParmChar + prop.Name);
                    }
                    else
                    {
                        sb.Append("," + prop.Name);
                        sb.Append("=");
                        sb.Append(DbHelper.DbParmChar + prop.Name);
                    }
                }
            }
            sb.Append(" Where ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
            return(sb);
        }
示例#4
0
        /// <summary>
        /// 拼接 查询最大数 SQL语句
        /// </summary>
        /// <param name="propertyName">属性字段</param>
        /// <returns></returns>
        public static StringBuilder SelectMaxSql <T>(string propertyName) where T : new()
        {
            string tableName = DatabaseCommon.GetTableName <T>().ToString(); //获取表名

            return(new StringBuilder("SELECT MAX(" + propertyName + ") FROM " + tableName + "  WHERE 1=1 "));
        }
示例#5
0
        /// <summary>
        /// 拼接 查询条数 SQL语句
        /// </summary>
        /// <returns></returns>
        public static StringBuilder SelectCountSql <T>() where T : new()
        {
            string tableName = DatabaseCommon.GetTableName <T>().ToString(); //获取表名

            return(new StringBuilder("SELECT Count(1) FROM " + tableName + " WHERE 1=1 "));
        }