/// <summary> /// /// </summary> /// <param name="objName"></param> /// <param name="obj"></param> /// <returns></returns> public static cat_Base Create(string objName, cat_Base obj) { // init insert query var sql = @"INSERT INTO [{0}] ({1}) VALUES ({2}); SELECT SCOPE_IDENTITY();"; // get object type var type = obj.GetType(); // get array of property info var arrPropertyInfo = type.GetProperties(); // init array of fields name & params name // except index 0 for Id field var arrFieldNames = new List <string>(); var arrParamNames = new List <string>(); var arrParamValues = new List <object>(); foreach (var p in arrPropertyInfo) { if (p.Name.ToLower() != "id" && p.CanWrite) { arrFieldNames.Add("[{0}]".FormatWith(p.Name)); arrParamNames.Add("@{0}".FormatWith(p.Name)); arrParamValues.Add(type.InvokeMember(p.Name, BindingFlags.GetProperty, null, obj, null)); } } // create insert sql sql = sql.FormatWith(objName, string.Join(",", arrFieldNames), string.Join(",", arrParamNames)); // execute var objReturn = SQLHelper.ExecuteScalar(sql, arrParamNames.ToArray(), arrParamValues.ToArray()); if (objReturn != null && Convert.ToInt32(objReturn) > 0) { obj.Id = Convert.ToInt32(objReturn); return(obj); } return(null); }
/// <summary> /// /// </summary> /// <param name="objName"></param> /// <param name="obj"></param> /// <returns></returns> public static cat_Base Update(string objName, cat_Base obj) { // init insert query var sql = @"UPDATE [{0}] SET {1} WHERE [Id]='{2}'"; // get object type var type = Type.GetType("Web.Core.Object.Catalog.{0}".FormatWith(objName)); if (type != null) { // get array of property info var arrPropertyInfo = type.GetProperties(); // init array of fields name & params name // except index 0 for Id field var arrExpressions = new List <string>(); var arrParamNames = new List <string>(); var arrParamValues = new List <object>(); foreach (var p in arrPropertyInfo) { if (p.Name.ToLower() != "id" && p.CanWrite) { arrExpressions.Add(string.Format("[{0}]=@{0}", p.Name)); arrParamNames.Add("@{0}".FormatWith(p.Name)); arrParamValues.Add(type.InvokeMember(p.Name, BindingFlags.GetProperty, null, obj, null)); } } // create update sql sql = sql.FormatWith(objName, string.Join(",", arrExpressions), obj.Id); // execute var rowEffected = SQLHelper.ExecuteNonQuery(sql, arrParamNames.ToArray(), arrParamValues.ToArray()); return(rowEffected > 0 ? obj : null); } return(null); }
/// <summary> /// /// </summary> /// <param name="objName"></param> /// <param name="model"></param> /// <returns></returns> public static CatalogModel Update(string objName, CatalogModel model) { // get existed by name var existedEntity = GetByName(objName, model.Name); // check existed if (existedEntity == null || existedEntity.Id == model.Id) { var entity = new cat_Base(); model.FillEntity(ref entity); return(new CatalogModel(objName, cat_BaseServices.Update(objName, entity))); } // report name existed return(null); }