public int Add(TEntityType item) { var props = _mapper.GetProperties(item); var attributes = from m in props select m.Name; var parameterlist = from m in attributes select "@" + m; string cmd = "insert into " + item.GetType().Name + "(" + String.Join(",", attributes) + ")" + "values(" + string.Join(",", parameterlist) + ")"; var MyType = typeof(TEntityType); List <SqlParameter> sqlParameters = new List <SqlParameter>(); foreach (var attr in attributes) { PropertyInfo prop = MyType.GetProperty(attr); object value = prop.GetValue(item); sqlParameters.Add(new SqlParameter(attr, value)); } return(SqlHelpers.ModifyDatabase(_connection, cmd, sqlParameters)); }
public int Update(TEntityType item) { var props = _mapper.GetProperties(item); List <SqlParameter> sqlParameters = new List <SqlParameter>(); var keys = from prop in typeof(TEntityType). GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(EntityKey))) select prop.Name; var myType = typeof(TEntityType); var keyWhere = new List <string>(); foreach (var key in keys) { var obj = myType.GetProperty(key).GetValue(item); keyWhere.Add( key + "=@" + key ); sqlParameters.Add(new SqlParameter(key, obj)); } var attributes = from m in props select m.Name; var parameterlist = from m in props select m.Name + "=@" + m.Name; string cmd = "update " + item.GetType().Name + " set " + String.Join(" ,", parameterlist) + " where " + string.Join(" and ", keyWhere); foreach (var attr in attributes) { PropertyInfo prop = myType.GetProperty(attr); object value = prop.GetValue(item); sqlParameters.Add(new SqlParameter(attr, value)); } return(SqlHelpers.ModifyDatabase(_connection, cmd, sqlParameters)); }