public static void ExecuteNonQuery(this IDatabaseAccessor instance, string commandText, dynamic parameterValues) { using (SqlConnection connection = instance.CreateConnection()) { string outputValue = null; connection.Open(); SqlCommand command = instance.CreateCommand(connection, commandText, CommandType.StoredProcedure); IEnumerable <PropertyInfo> properties = ((Type)parameterValues.GetType()).GetProperties().Where(property => property.CanRead); properties.ForEach(property => { object value = property.GetValue(parameterValues, null); SqlDbType?valType = null; ParameterDirection direction = ParameterDirection.Input; string parameterName = property.Name; if (parameterName.StartsWith("_")) { direction = ParameterDirection.InputOutput; parameterName = parameterName.Substring(1); outputValue = parameterName; } if (!parameterName.Equals("IncludeDeleted", StringComparison.OrdinalIgnoreCase)) { command.SetParameter(parameterName, value, direction, valType); } else { command.SetIncludeDeletedParameter((bool)value); } }); var reader = command.ExecuteNonQuery(); } }
public static void SetParameters <T>(this SqlCommand command, T instance) { IEnumerable <PropertyInfo> properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.FlattenHierarchy).Where(property => property.CanUsePropertyToSave()); properties.ForEach(property => { object value = property.GetValue(instance, null); SqlDbType?valType = null; ParameterDirection direction = ParameterDirection.Input; if ((property.PropertyType == typeof(Guid) || property.PropertyType == typeof(Guid?)) && (value == null || value.Equals(Guid.Empty))) { valType = SqlDbType.UniqueIdentifier; direction = ParameterDirection.InputOutput; } command.SetParameter(property.Name, value, direction, valType); }); }
public static void SetParameters(this SqlCommand command, dynamic values) { if (values == null) { throw new ArgumentNullException("values"); } IEnumerable <PropertyInfo> properties = ((Type)values.GetType()).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.FlattenHierarchy).Where(property => property.CanUsePropertyToSave()); properties.ForEach(property => { object value = property.GetValue(values, null); SqlDbType?valType = null; ParameterDirection direction = ParameterDirection.Input; if (property.Name.StartsWith("_")) { direction = ParameterDirection.InputOutput; } command.SetParameter(property.Name, value, direction, valType); }); }
public static void SetParameterDirection(this SqlCommand command, string parameterName, ParameterDirection direction) { SqlParameter parameter = command.SetParameter(parameterName, null); parameter.Direction = direction; }
public static SqlParameter SetParameter(this SqlCommand command, string parameterName, object value, ParameterDirection direction) { return(command.SetParameter(parameterName, value, direction, null)); }
public static SqlParameter SetParameter(this SqlCommand command, string parameterName, object value) { return(command.SetParameter(parameterName, value.IsDefault() ? null : (object)value, ParameterDirection.Input, null)); }