public static void Fill(object dataObject, SqlDataReader dr) { Type objectType = dataObject.GetType(); PropertyInfo[] objectProperties = objectType.GetProperties(); PropertyInfo isFillingPropertyInfo = objectType.GetProperty("IsFilling"); isFillingPropertyInfo.SetValue(dataObject, true, null); foreach (PropertyInfo propertyInfo in objectProperties) { YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute sqlAttribute = (YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)); if (sqlAttribute != null) { switch (sqlAttribute.SqlDbType) { case SqlDbType.VarChar: case SqlDbType.NVarChar: case SqlDbType.NText: propertyInfo.SetValue(dataObject, BaseData.GetStringValue(sqlAttribute.FieldName, dr), null); break; case SqlDbType.Int: case SqlDbType.BigInt: propertyInfo.SetValue(dataObject, BaseData.GetIntValue(sqlAttribute.FieldName, dr), null); break; case SqlDbType.Float: try { propertyInfo.SetValue(dataObject, BaseData.GetDoubleValue(sqlAttribute.FieldName, dr), null); } catch (Exception) { MessageBox.Show(propertyInfo.Name); } break; case SqlDbType.Bit: propertyInfo.SetValue(dataObject, BaseData.GetBoolValue(sqlAttribute.FieldName, dr), null); break; case SqlDbType.DateTime: case SqlDbType.SmallDateTime: propertyInfo.SetValue(dataObject, BaseData.GetDateTimeValue(sqlAttribute.FieldName, dr), null); break; default: MessageBox.Show("Error Writing data from DataReader DataType " + sqlAttribute.SqlDbType + " " + propertyInfo.Name + " " + objectType.Name); break; } } } isFillingPropertyInfo.SetValue(dataObject, false, null); MethodInfo notifyPropertyChangeMethodInfo = objectType.GetMethod("NotifyPropertyChanged"); object[] parameters = new object[1]; parameters[0] = string.Empty; notifyPropertyChangeMethodInfo.Invoke(dataObject, parameters); }
public static void Update(object dataObject) { Type objectType = dataObject.GetType(); PropertyInfo propertyInfoPropertyChangedList = objectType.GetProperty("PropertyChangedList"); List <PropertyChangedItem> propertyChangedList = (List <PropertyChangedItem>)propertyInfoPropertyChangedList.GetValue(dataObject, null); SqlCommand cmd = new SqlCommand(); string sql = string.Empty; if (propertyChangedList.Count != 0) { YellowstonePathology.Business.CustomAttributes.SqlTableAttribute tableAttribute = (YellowstonePathology.Business.CustomAttributes.SqlTableAttribute)Attribute.GetCustomAttribute(objectType, typeof(YellowstonePathology.Business.CustomAttributes.SqlTableAttribute)); sql = "Update " + tableAttribute.TableName + " Set "; SqlParameter keyParam = new SqlParameter(); keyParam.ParameterName = "@" + tableAttribute.KeyFieldName; keyParam.Size = tableAttribute.KeyFieldLength; PropertyInfo keyPropertyInfo = objectType.GetProperty(tableAttribute.KeyFieldName); object keyValue = keyPropertyInfo.GetValue(dataObject, null); SetParameterValue(keyParam, tableAttribute.KeyFieldSqlDbType, keyValue); cmd.Parameters.Add(keyParam); foreach (PropertyChangedItem item in propertyChangedList) { PropertyInfo propertyInfo = objectType.GetProperty(item.PropertyName); object value = propertyInfo.GetValue(dataObject, null); YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute fieldAttribute = (YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)); sql += fieldAttribute.FieldName + " = @" + fieldAttribute.FieldName + ", "; SqlParameter param = new SqlParameter(); param.ParameterName = "@" + fieldAttribute.FieldName; SetParameterValue(param, fieldAttribute.SqlDbType, value); cmd.Parameters.Add(param); } sql = sql.Remove(sql.Length - 2, 1); sql += "Where " + tableAttribute.KeyFieldName + " = @" + tableAttribute.KeyFieldName; using (SqlConnection cn = new SqlConnection(BaseData.SqlConnectionString)) { cn.Open(); cmd.Connection = cn; cmd.CommandText = sql; cmd.ExecuteNonQuery(); } propertyChangedList.Clear(); } }
public static void Insert(object dataObject) { Type objectType = dataObject.GetType(); PropertyInfo propertyInfoPropertyChangedList = objectType.GetProperty("PropertyChangedList"); List <PropertyChangedItem> propertyChangedList = (List <PropertyChangedItem>)propertyInfoPropertyChangedList.GetValue(dataObject, null); SqlCommand cmd = new SqlCommand(); string sql = string.Empty; string fieldList = string.Empty; string valueList = string.Empty; if (propertyChangedList.Count != 0) { YellowstonePathology.Business.CustomAttributes.SqlTableAttribute tableAttribute = (YellowstonePathology.Business.CustomAttributes.SqlTableAttribute)Attribute.GetCustomAttribute(objectType, typeof(YellowstonePathology.Business.CustomAttributes.SqlTableAttribute)); sql = "Insert " + tableAttribute.TableName + " "; foreach (PropertyChangedItem item in propertyChangedList) { PropertyInfo propertyInfo = objectType.GetProperty(item.PropertyName); object value = propertyInfo.GetValue(dataObject, null); YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute fieldAttribute = (YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)); fieldList += fieldAttribute.FieldName + ", "; valueList += "@" + fieldAttribute.FieldName + ", "; SqlParameter param = new SqlParameter(); param.ParameterName = "@" + fieldAttribute.FieldName; SetParameterValue(param, fieldAttribute.SqlDbType, value); cmd.Parameters.Add(param); } fieldList = fieldList.Remove(fieldList.Length - 2, 1); valueList = valueList.Remove(valueList.Length - 2, 1); sql += "(" + fieldList + ") values (" + valueList + ") "; using (SqlConnection cn = new SqlConnection(BaseData.SqlConnectionString)) { cn.Open(); cmd.Connection = cn; cmd.CommandText = sql; cmd.ExecuteNonQuery(); } propertyChangedList.Clear(); } }
public void SetupSpellCheckingList(Type objectType) { YellowstonePathology.Business.CustomAttributes.SqlTableAttribute tableAttribute = (YellowstonePathology.Business.CustomAttributes.SqlTableAttribute)Attribute.GetCustomAttribute(objectType, typeof(YellowstonePathology.Business.CustomAttributes.SqlTableAttribute)); if (tableAttribute != null) { if (tableAttribute.HasFieldsToSpellCheck == true) { PropertyInfo[] propertyList = objectType.GetProperties(); foreach (PropertyInfo property in propertyList) { YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute fieldAttribute = (YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)Attribute.GetCustomAttribute(property, typeof(YellowstonePathology.Business.CustomAttributes.SqlFieldAttribute)); if (fieldAttribute != null) { if (fieldAttribute.NeedsSpellCheck == true) { SpellCheckListItem item = new SpellCheckListItem(); item.Property = property; item.FieldAttribute = fieldAttribute; this.m_SpellCheckList.Add(item); } } } } } }