/// <summary> /// 得到Model组成SQL对象 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <returns>Model组成SQL对象</returns> public static TModel GetUpdateTModel <T>(T entity) { TModel tmodel = new TModel(); var type = typeof(T); var properties = type.GetProperties().Where(p => !GetAttributes(p).OfType <NonFieldAttribute>().Any()); ParameterizedQueryBuilder parms = new ParameterizedQueryBuilder(); StringBuilder strSql = new StringBuilder(); strSql.Append("Update " + type.Name + " set "); string fields = ""; string primarykey = ""; string primaryvalue = ""; int loop = 0; foreach (var p in properties) { var name = GetFieldname(p); if (name.IndexOf("OID") == -1) { fields += name + "={" + loop + "},"; parms.AppendParameter(p.GetValue(entity, null)); loop++; } else { primarykey = name; primaryvalue = p.GetValue(entity, null).ToString(); } } strSql.Append(fields.Substring(0, fields.Length - 1) + " where " + primarykey + "=" + primaryvalue); tmodel.SQL = strSql.ToString(); tmodel.ParameterValues = parms.CreateQuery().ParameterValues; return(tmodel); }
/// <summary> /// 得到Model组成SQL对象 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="entity">实体</param> /// <returns>Model组成SQL对象</returns> public static TModel GetInsertTModel <T>(T entity) { TModel tmodel = new TModel(); var type = typeof(T); var properties = type.GetProperties().Where(p => !GetAttributes(p).OfType <NonFieldAttribute>().Any()); ParameterizedQueryBuilder parms = new ParameterizedQueryBuilder(); StringBuilder strSql = new StringBuilder(); strSql.Append("insert into " + type.Name + "("); string fields = ""; foreach (var p in properties) { var name = GetFieldname(p); if (name.IndexOf("NOID") == -1) { fields += name + ","; parms.AppendParameter(p.GetValue(entity, null)); } } strSql.Append(fields.Substring(0, fields.Length - 1) + ") values({...});select @@IDENTITY"); tmodel.SQL = strSql.ToString(); tmodel.ParameterValues = parms.CreateQuery().ParameterValues; return(tmodel); }