Exemplo n.º 1
0
        /// <summary>
        /// Sets the SQL parameter value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameter">The parameter.</param>
        /// <param name="value">The value.</param>
        /// <param name="mode">The mode.</param>
        /// <exception cref="ArgumentNullException"><paramref name="parameter"/> is <see langword="null" />.</exception>
        /// <exception cref="DatabaseSchemaException"><para>The type <typeparamref name="T" /> is invalid for the <see cref="Direction" />.</para>
        /// <para>-or-</para>
        /// <para>The type <typeparamref name="T" /> was unsupported.</para>
        /// <para>-or-</para>
        /// <para>A fatal error occurred.</para>
        /// <para>-or-</para>
        /// <para>The object exceeded the SQL type's maximum <see cref="SqlTypeSize">size</see>.</para>
        /// <para>-or-</para>
        /// <para>The serialized object was truncated.</para>
        /// <para>-or-</para>
        /// <para>Unicode characters were found and only ASCII characters are supported in the SQL type.</para>
        /// <para>-or-</para>
        /// <para>The date was outside the range of accepted dates for the SQL type.</para></exception>
        public void SetSqlParameterValue <T>(
            [NotNull] SqlParameter parameter,
            T value,
            TypeConstraintMode mode = TypeConstraintMode.Warn)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (value == null)
            {
                parameter.Value = Type.IsTable ? null : DBNull.Value;
                return;
            }

            IOut output = value as IOut;

            if (output != null)
            {
                bool   hasInput = output?.InputValue.IsAssigned ?? false;
                string dir      = hasInput ? "input/output" : "output";
                switch (Direction)
                {
                case ParameterDirection.Input:
                    throw new DatabaseSchemaException(
                              LoggingLevel.Error,
                              () => "Cannot pass {0} value to input only parameter {1}",
                              dir,
                              FullName);

                case ParameterDirection.Output:
                case ParameterDirection.ReturnValue:
                    if (hasInput)
                    {
                        throw new DatabaseSchemaException(
                                  LoggingLevel.Error,
                                  () => "Cannot pass {0} value to output only parameter {1}",
                                  dir,
                                  FullName);
                    }
                    break;
                }

                output.SetParameter(parameter);

                if (hasInput)
                {
                    parameter.Value = Type.CastCLRValue(output.InputValue.Value, output.Type, mode);
                }
            }
            else
            {
                parameter.Value = Type.CastCLRValue(value, mode);
            }
        }
Exemplo n.º 2
0
 public object CastCLRValue <T>(T value, TypeConstraintMode mode = TypeConstraintMode.Warn)
 {
     // TODO What about SqlMetaData.Adjust()?
     return(Type.CastCLRValue(value, mode));
 }