Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }