Created
July 16, 2013 13:03
-
-
Save Alxandr/6008481 to your computer and use it in GitHub Desktop.
Convert records in a record-set to a given type. Usage: myDbReader.Get<string>("userName")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal static class DatabaseExtensions | |
| { | |
| public static bool HasColumn(this IDataRecord reader, int columnNumber) | |
| { | |
| return reader.FieldCount > columnNumber && columnNumber >= 0; | |
| } | |
| public static bool HasColumn(this IDataRecord reader, string columnName) | |
| { | |
| for (int i = 0, l = reader.FieldCount; i < l; i++) | |
| { | |
| if (reader.GetName(i).Equals(columnName, StringComparison.OrdinalIgnoreCase)) | |
| { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public static T Get<T>(this IDataRecord reader, int columnNumber, T defaultValue = default(T)) | |
| { | |
| return HasColumn(reader, columnNumber) ? | |
| TryConvert<T>(reader[columnNumber], defaultValue) : | |
| defaultValue; | |
| } | |
| public static T Get<T>(this IDataRecord reader, string columnName, T defaultValue = default(T)) | |
| { | |
| return HasColumn(reader, columnName) ? | |
| TryConvert<T>(reader[columnName], defaultValue) : | |
| defaultValue; | |
| } | |
| private static T TryConvert<T>(object obj, T defaultValue) | |
| { | |
| object result; | |
| if (TryConvert(obj, typeof(T), out result)) | |
| { | |
| if (result == null) | |
| { | |
| return default(T); | |
| } | |
| if (typeof (T) == typeof (string)) | |
| result = ((string) result).TrimEnd(); | |
| return (T)result; | |
| } | |
| return defaultValue; | |
| } | |
| private static bool TryConvert(object obj, Type targetType, out object value) | |
| { | |
| if(obj == null || obj is DBNull) | |
| { | |
| value = null; | |
| return true; | |
| } | |
| CustomConvert(ref obj, targetType); | |
| if(targetType.IsInstanceOfType(obj)) | |
| { | |
| value = obj; | |
| return true; | |
| } | |
| var converter = TypeDescriptor.GetConverter(obj.GetType()); | |
| if (converter.CanConvertTo(targetType)) | |
| { | |
| value = converter.ConvertTo(obj, targetType); | |
| return true; | |
| } | |
| value = null; | |
| return false; | |
| } | |
| private static void CustomConvert(ref object obj, Type targetType) | |
| { | |
| //Type objType = obj.GetType(); | |
| if (targetType == typeof(Int64)) | |
| { | |
| try { object temp = Convert.ToInt64(obj, CultureInfo.InvariantCulture); obj = temp; } | |
| catch (FormatException) { /* ignore. conversion failed. */ } | |
| catch (InvalidCastException) { /* ignore. conversion failed. */ } | |
| catch (OverflowException) { /* ignore. conversion failed. */ } | |
| } | |
| try | |
| { | |
| Object result; | |
| Type underlaying = Enum.GetUnderlyingType(targetType); | |
| if (TryConvert(obj, underlaying, out result)) | |
| { | |
| // Type is an enum and converts | |
| obj = Enum.ToObject(targetType, result); | |
| } | |
| } | |
| catch (ArgumentException) | |
| { | |
| // Type is not enum. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment