Beispiel #1
0
        private static IList <DataParameterInfo> GetParamsFromObject(object dataParams)
        {
            if (dataParams == null)
            {
                return(new List <DataParameterInfo>());
            }

            IList <DataParameterInfo> dataParameters = new List <DataParameterInfo>();

            PropertyInfo[] properties = dataParams.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (!property.CanRead || property.GetCustomAttribute <NotMappedAttribute>() != null)
                {
                    continue;
                }

                DataParameterInfo dataParamInfo = new DataParameterInfo();
                ColumnAttribute   column        = property.GetCustomAttribute <ColumnAttribute>();

                dataParamInfo.Name = column != null ? column.Name : property.Name;

                object propertyValue = property.GetValue(dataParams);
                dataParamInfo.Value = IsEnum(property) ? GetEnumValue(propertyValue) : propertyValue;

                dataParamInfo.IsStructured = !IsSimpleType(property.PropertyType);
                dataParamInfo.DataType     = string.Empty;
                dataParameters.Add(dataParamInfo);
            }

            return(dataParameters);
        }
Beispiel #2
0
        private IDbDataParameter GetDbParam(DataParameterInfo paramInfo, bool isOutput = false)
        {
            SqlParameter parameter = new SqlParameter {
                ParameterName = paramInfo.Name
            };

            if (paramInfo.Value == null)
            {
                parameter.Value = DBNull.Value;
            }
            else if (paramInfo.Value is bool)
            {
                parameter.Value = (bool)paramInfo.Value ? 1 : 0;
            }
            else if (paramInfo.Value is Enum)
            {
                parameter.Value = paramInfo.Value.ToString();
            }
            else
            {
                parameter.Value = paramInfo.Value;
            }

            if (paramInfo.Value is string)
            {
                parameter.Size = int.MaxValue;
            }

            if (paramInfo.IsStructured)
            {
                parameter.SqlDbType = SqlDbType.Structured;
                parameter.TypeName  = paramInfo.DataType;
                parameter.Value     = GetDataTable(paramInfo.Value);
            }

            if (isOutput)
            {
                parameter.Direction = ParameterDirection.Output;
            }

            return(parameter);
        }
Beispiel #3
0
        /// <summary>
        /// Fills the output parameters.
        /// </summary>
        /// <param name="dataParams">The data parameters.</param>
        public void FillOutputParams(object dataParams)
        {
            if (dataParams == null)
            {
                return;
            }

            PropertyInfo[] properties = dataParams.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (!property.CanRead || property.GetCustomAttribute <NotMappedAttribute>() != null)
                {
                    continue;
                }

                ColumnAttribute   column    = property.GetCustomAttribute <ColumnAttribute>();
                DataParameterInfo paramInfo = _outParams.Single(pi => pi.Name == (column == null ? property.Name : column.Name));

                object dataBaseValue = ((DbParameter)_dataBaseCommand.Parameters[paramInfo.Name]).Value;
                object propertyValue = paramInfo.Value is bool?Convert.ChangeType(dataBaseValue, TypeCode.Boolean) : dataBaseValue;

                if (property.CanWrite)
                {
                    property.SetValue(dataParams, propertyValue);
                }
                else
                {
                    string    fieldName = $"<{paramInfo.Name}>i__Field";
                    FieldInfo field     = dataParams.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);

                    if (field != null)
                    {
                        field.SetValue(dataParams, propertyValue);
                    }
                }
            }
        }