/// <summary> /// use for update command/query /// create list of key-value pair like [ propertyname = propertyvalue ,] and so on /// dont consider primary key property in list /// </summary> /// <param name="mytype">type of class [ex person class or student etc]</param> private void SetUpdatePropertise(MyType myType) { try { UpdatePropertiesNamesValuesPair = ""; SetPimaryKeyName(myType); foreach (PropertyInfo property in myType.GetType().GetProperties()) { if (property.Name != PrimaryKeyNameInSourceCode && !AttributeHeler.CheckIgnoreColumn(property)) { TypeCode typeCode = Type.GetTypeCode(property.PropertyType); string Name = AttributeHeler.GetColumnName(property);// property.Name; Object value = property.GetValue(myType, null); if (Name != PrimaryKeyNameInDB) { UpdatePropertiesNamesValuesPair += (Name + "='" + value + "',"); } } } UpdatePropertiesNamesValuesPair = UpdatePropertiesNamesValuesPair.Remove(UpdatePropertiesNamesValuesPair.Length - 1); } catch (Exception ex) { throw ex; } }
/// <summary> /// create comma separated propertyname list /// if includeprimary is true then primary key propery also consider in list /// else primary key property is remove from list /// </summary> /// <param name="mytype">type of class [ex person class or student etc]</param> /// <param name="includeprimary">weather primary key property should consider in list or not if yes : consider , if false : removed from list</param> private void SetPropertiesNames(MyType myType, bool includePrimary = true) { foreach (var property in myType.GetType().GetProperties()) { if (!AttributeHeler.CheckIgnoreColumn(property)) { if (includePrimary) { string Name = AttributeHeler.GetColumnName(property); PropertiesNames += (Name + ","); } else if (!AttributeHeler.IsPrimaryKeyAttribute(property)) { string Name = AttributeHeler.GetColumnName(property); PropertiesNames += (Name + ","); } } } PropertiesNames = PropertiesNames.Remove(PropertiesNames.Length - 1); }
/// <summary> /// Helper method /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dr"></param> /// <returns></returns> private static T GetItem <T>(DataRow dr) { Type temp = typeof(T); T obj = Activator.CreateInstance <T>(); foreach (DataColumn column in dr.Table.Columns) { foreach (PropertyInfo pro in temp.GetProperties()) { string primaryKeyName = AttributeHeler.GetColumnName(pro); if (primaryKeyName == column.ColumnName && dr[column.ColumnName] != DBNull.Value) { pro.SetValue(obj, dr[column.ColumnName], null); } else { continue; } } } return(obj); }
/// <summary> /// return primary key name /// </summary> /// <param name="mytype"></param> /// <returns></returns> private string SetPimaryKeyName(MyType myType) { var isContainsPrimarKey = ContainsPrimaryKey(myType); if (isContainsPrimarKey) { //foreach loop can be optimised foreach (PropertyInfo property in myType.GetType().GetProperties()) { if (AttributeHeler.IsPrimaryKeyAttribute(property)) { PrimaryKeyNameInSourceCode = property.Name; string primaryKeyName = AttributeHeler.GetColumnName(property); PrimaryKeyNameInDB = primaryKeyName; return(primaryKeyName); } } } else { throw new Exception(ExceptionMessage.PK_NOT_EXIST); } throw new Exception("Error occured in SetPkName function"); }