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); } } } } } }
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(); } }