public virtual bool Update(string tableName, object obj, long groups) { QL.Database.DbCommandBuilder builder = this.DbHelper.CreateDbCommandBuilder(tableName); Type type = typeof(DbFieldAttribute); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj); if (properties.Count == 0) { return(false); } foreach (PropertyDescriptor descriptor in properties) { DbFieldAttribute attribute = (DbFieldAttribute)descriptor.Attributes[type]; if (attribute != null) { string name = string.IsNullOrEmpty(attribute.Name) ? descriptor.Name : attribute.Name; if ((!attribute.Identity && !attribute.ReadOnly) && attribute.AllowOperation(groups, false)) { builder.AddField(name, this.DbHelper.CreateDbParameter(name, attribute.GetDbType(descriptor.PropertyType), attribute.Size, descriptor.GetValue(obj))); } if (attribute.PrimaryKey) { builder.AddCondition(name, this.DbHelper.CreateDbParameter(name, attribute.GetDbType(descriptor.PropertyType), attribute.Size, descriptor.GetValue(obj))); } } } return(builder.Update() > 0); }
internal static Dictionary <string, PropertyDescriptor> GetDbFileds(this Type type) { Type type2 = typeof(DbFieldAttribute); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(type); if (properties.Count == 0) { return(null); } Dictionary <string, PropertyDescriptor> dictionary = new Dictionary <string, PropertyDescriptor>(properties.Count, StringComparer.OrdinalIgnoreCase); foreach (PropertyDescriptor descriptor in properties) { DbFieldAttribute attribute = (DbFieldAttribute)descriptor.Attributes[type2]; if (attribute != null) { string key = string.IsNullOrEmpty(attribute.Name) ? descriptor.Name : attribute.Name; if (!dictionary.ContainsKey(key)) { dictionary.Add(key, descriptor); } } } return(dictionary); }
public virtual bool Insert(string tableName, object obj, long groups) { QL.Database.DbCommandBuilder builder = this.DbHelper.CreateDbCommandBuilder(tableName); PropertyDescriptor descriptor = null; Type type = typeof(DbFieldAttribute); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj); if (properties.Count == 0) { return(false); } foreach (PropertyDescriptor descriptor2 in properties) { DbFieldAttribute attribute = (DbFieldAttribute)descriptor2.Attributes[type]; if (attribute != null) { if (attribute.Identity) { descriptor = descriptor2; } else if (!attribute.ReadOnly && attribute.AllowOperation(groups, true)) { string name = string.IsNullOrEmpty(attribute.Name) ? descriptor2.Name : attribute.Name; builder.AddField(name, this.DbHelper.CreateDbParameter(name, attribute.GetDbType(descriptor2.PropertyType), attribute.Size, descriptor2.GetValue(obj))); } } } long num = 0L; if (builder.HasFields) { if (descriptor != null) { num = this.ExecuteIdentity(builder.InsertCommandText, builder.Parameters); if (num > 0L) { object obj2 = num; if (descriptor.PropertyType != typeof(long)) { obj2 = num.As(descriptor.PropertyType, num); } descriptor.SetValue(obj, obj2); } } else { num = builder.Insert(); } } return(num > 0L); }