Ejemplo n.º 1
0
        /// <summary>
        /// Adds parameters from an anonymous type
        /// </summary>
        /// <param name="command">The command to add the properties to</param>
        /// <param name="parameters">The anonymous object that contains the parameters</param>
        public static void AddParameters(this IDbCommand command, object parameters)
        {
            if (parameters == null)
            {
                return;
            }

            var paramType = parameters.GetType();

            foreach (var propertyInfo in paramType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                var parameter = command.CreateParameter();
                parameter.ParameterName = propertyInfo.Name;
                parameter.Value         = propertyInfo.GetValue(parameters) ?? DBNull.Value;
                parameter.DbType        = TypeMapping.GetDbType(propertyInfo.PropertyType);
                command.Parameters.Add(parameter);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add parameters from an anonymous type and add to where clause
        /// </summary>
        /// <param name="command">THe command to add the parameters and where clause to</param>
        /// <param name="parameters">The anonymous object that contains the parameters</param>
        /// <remarks>Only works for sql command text. appends the where clause to the end with the property names</remarks>
        public static void AddParametersWithWhere(this IDbCommand command, object parameters)
        {
            if (parameters == null)
            {
                return;
            }

            var paramType = parameters.GetType();
            var whereList = new List <string>();

            foreach (var propertyInfo in paramType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                var parameter = command.CreateParameter();
                parameter.ParameterName = propertyInfo.Name;
                parameter.Value         = propertyInfo.GetValue(parameters) ?? DBNull.Value;
                parameter.DbType        = TypeMapping.GetDbType(propertyInfo.PropertyType);
                command.Parameters.Add(parameter);

                if (parameter.Value is string valStr && valStr.Contains("%"))
                {
                    whereList.Add($"[{propertyInfo.Name}] LIKE @{propertyInfo.Name}");
                }
Ejemplo n.º 3
0
 /// <summary>
 /// Add a parameter to a command
 /// </summary>
 /// <param name="command">The command to add the parameter to</param>
 /// <param name="name">The name of the parameter</param>
 /// <param name="value">The value of the parameter</param>
 public static void AddParameter(this IDbCommand command, string name, object value)
 {
     AddParameter(command, name, TypeMapping.GetDbType(value.GetType()), value);
 }