protected static void AddParameters <X>(X value, SqlParameterCollection collection) where X : class { foreach (var prop in typeof(X).GetProperties().Where(p => !p.IsMarkedWith <NotAParamAttribute>() && !p.IsMarkedWith <IsOutputParamAttribute>())) { var propValue = prop.GetValue(value); if (prop.PropertyType == typeof(int)) { collection.AddInt(prop.Name, (int)propValue); } else if (prop.PropertyType == typeof(int?)) { collection.AddNullableInt(prop.Name, (int?)propValue); } else if (prop.PropertyType == typeof(short)) { collection.AddSmallInt(prop.Name, (short)propValue); } else if (prop.PropertyType == typeof(short?)) { collection.AddNullableSmallInt(prop.Name, (short?)propValue); } else if (prop.PropertyType == typeof(DataTable)) { collection.AddTable(prop.Name, (DataTable)propValue); } else if (prop.PropertyType == typeof(DateTime?)) { collection.AddNullableDateTime(prop.Name, (DateTime?)propValue); } else if (prop.PropertyType == typeof(DateTime)) { collection.AddNullableDateTime(prop.Name, (DateTime)propValue); } else if (prop.PropertyType == typeof(TimeSpan?)) { collection.AddNullableTime(prop.Name, (TimeSpan?)propValue); } else if (prop.PropertyType == typeof(TimeSpan)) { collection.AddNullableTime(prop.Name, (TimeSpan)propValue); } else if (prop.PropertyType == typeof(decimal?)) { collection.AddNullableMoney(prop.Name, (decimal?)propValue); } else if (prop.PropertyType == typeof(bool)) { collection.AddBit(prop.Name, (bool)propValue); } else if (prop.PropertyType == typeof(bool?)) { if (propValue != null) { collection.AddBit(prop.Name, (bool)propValue); } else if (prop.IsMarkedWith <DefaultIfNullAttribute>()) { bool defaultIfNull = (bool)prop.GetAttributes <DefaultIfNullAttribute>().First().DefaultValue; collection.AddBitWithDefault(prop.Name, (bool?)propValue, defaultIfNull); } else { collection.AddWithValue(prop.Name, DBNull.Value); } } else if (prop.PropertyType == typeof(string)) { if (prop.IsMarkedWith <MaxLengthAttribute>()) { int maxLength = prop.GetAttributes <MaxLengthAttribute>().First().Length; collection.AddNVarChar(prop.Name, (string)propValue, maxLength); } else { collection.AddNVarCharMax(prop.Name, (string)propValue); } } else { if (propValue != null) { collection.AddWithValue(prop.Name, propValue); } } } }