Exemplo n.º 1
0
        private bool SetValue(object value)
        {
            var    result = false;
            object val    = null;

            if (KeyType.Primary.Equals(KeyType))
            {
                if (this.State != ColumnState.RAW && this.State != ColumnState.ASSIGNED)
                {
                    throw new Exception(string.Format(GeneralMessages.ERR_COLUMN_TRY_TO_CHANGE_PRIMARY, this.ID));
                }

                var str = DatabaseCore.Convert2 <string>(value, out result);
                if (!result || string.IsNullOrWhiteSpace(str))
                {
                    throw new Exception(string.Format(GeneralMessages.ERR_COLUMN_SET_NULL_TO_PRIMARY, this.ID));
                }
            }
            if (State == ColumnState.ERROR)
            {
                throw new Exception(string.Format(GeneralMessages.ERR_COLUMN_IN_ERROR_STATE, this.ID));
            }

            if (value == DBNull.Value)
            {
                value = null;
            }
            if (value == null)
            {
                _value1 = null;
                result  = true;
            }
            else
            {
                if (this.DataType.Equals(value.GetType()))
                {
                    _value1 = value;
                    result  = true;
                }
                else
                {
                    if (this.DataType == typeof(string))
                    {
                        val = DatabaseCore.Convert2 <string>(value, out result);
                    }
                    if (this.DataType == typeof(int) || this.DataType == typeof(int?))
                    {
                        val = DatabaseCore.Convert2 <int>(value, out result);
                    }
                    if (this.DataType == typeof(long) || this.DataType == typeof(long?))
                    {
                        val = DatabaseCore.Convert2 <long>(value, out result);
                    }
                    if (this.DataType == typeof(decimal) || this.DataType == typeof(decimal?))
                    {
                        val = DatabaseCore.Convert2 <decimal>(value, out result);
                    }
                    if (this.DataType == typeof(DateTime) || this.DataType == typeof(DateTime?))
                    {
                        val = DatabaseCore.Convert2 <DateTime>(value, out result);
                    }
                    if (result)
                    {
                        _value1 = val;
                    }
                }
            }
            return(result);
        }