/// <summary> /// UpdateSql , 修改数据 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="entity">实体类</param> /// <param name="pkName">条件列名</param> /// <returns>Sql</returns> public static StringBuilder UpdateSql <T>(T entity, string pkName) { Type type = entity.GetType(); PropertyInfo[] properties = type.GetProperties(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(" Update "); stringBuilder.Append(type.Name); stringBuilder.Append(" Set "); bool flag = true; foreach (PropertyInfo propertyInfo in properties) { if (propertyInfo.GetValue((object)entity, (object[])null) != null && DbHelperSqlGeneration.GetKeyField <T>().ToString() != propertyInfo.Name) { if (flag) { flag = false; stringBuilder.Append(propertyInfo.Name); stringBuilder.Append("="); stringBuilder.Append(DbHelper.DbParmChar + propertyInfo.Name); } else { stringBuilder.Append("," + propertyInfo.Name); stringBuilder.Append("="); stringBuilder.Append(DbHelper.DbParmChar + propertyInfo.Name); } } } stringBuilder.Append(" Where ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName); return(stringBuilder); }
/// <summary> /// SelectSql , 查询前多少条数据根据实体类 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="Top">多少条</param> /// <returns>Sql</returns> public static StringBuilder SelectSql <T>(int Top) where T : new() { string name = typeof(T).Name; PropertyInfo[] properties = DbHelperSqlGeneration.GetProperties(new T().GetType()); StringBuilder stringBuilder = new StringBuilder(); foreach (PropertyInfo propertyInfo in properties) { stringBuilder.Append(propertyInfo.Name + ","); } if (stringBuilder.Length > 0) { stringBuilder.Remove(stringBuilder.ToString().Length - 1, 1); } return(new StringBuilder(string.Format("SELECT top {0} {1} FROM {2} WHERE 1=1 ", (object)Top, (object)stringBuilder.ToString(), (object)(name + " ")))); }