private static async Task <IList <T> > ReadToListOfValuesAsync <T>(this SqlCommand cmd, IList <T> list) { if (list == null) { list = new List <T>(); } var type = typeof(T); var isNullableValueType = type.IsNullableValueType(); var readerFlags = await GetReaderFlagsAndOpenConnectionAsync(cmd, CommandBehavior.SingleResult); using (var dr = await cmd.ExecuteReaderAsync(readerFlags).ConfigureAwait(false)) { if (isNullableValueType) { var underlyingType = type.GetUnderlyingType(); while (dr.Read()) { list.Add(dr.IsDBNull(0) ? default(T) : SqlDataReaderExtensions.GetValue <T>(dr, underlyingType)); } } else { while (dr.Read()) { list.Add(SqlDataReaderExtensions.GetValue <T>(dr, type)); } } } return(list); }
private static IList <T> ReadToListOfValues <T>(this SqlCommand cmd, IList <T> list) { if (list == null) { list = new List <T>(); } var type = typeof(T); var isNullableValueType = type.IsNullableValueType(); var readerFlags = GetReaderFlagsAndOpenConnection(cmd, CommandBehavior.SingleResult); using (var dr = cmd.ExecuteReader(readerFlags)) { if (isNullableValueType) { var underlyingType = Nullable.GetUnderlyingType(type) ?? type; while (dr.Read()) { list.Add(dr.IsDBNull(0) ? default(T) : SqlDataReaderExtensions.GetValue <T>(dr, underlyingType)); } } else { while (dr.Read()) { list.Add(SqlDataReaderExtensions.GetValue <T>(dr, type)); } } } return(list); }
public static async Task <T> ReadAsAsync <T>(this SqlCommand cmd) { if (typeof(T).IsSimpleType()) { return(await cmd.ReadToValueAsync <T>()); } var key = SqlDataReaderExtensions.GetAutoCreateObjectFuncKey <T>(cmd.CommandText); var autoMappingFunc = MappingManager.GetAutoCreateObjectFunc <T>(key); var readerFlags = await GetReaderFlagsAndOpenConnectionAsync(cmd, CommandBehavior.SingleRow); using (var dr = await cmd.ExecuteReaderAsync(readerFlags).ConfigureAwait(false)) { return(dr.Read() ? dr.CreateObject(autoMappingFunc, key) : default(T)); } }
public static T ReadAs <T>(this SqlCommand cmd) { if (typeof(T).IsSimpleType()) { return(cmd.ReadToValue <T>()); } var key = SqlDataReaderExtensions.GetAutoCreateObjectFuncKey <T>(cmd.CommandText); var autoMappingFunc = MappingManager.GetAutoCreateObjectFunc <T>(key); var readerFlags = GetReaderFlagsAndOpenConnection(cmd, CommandBehavior.SingleRow); using (var dr = cmd.ExecuteReader(readerFlags)) { return(dr.Read() ? dr.CreateObject(autoMappingFunc, key) : default(T)); } }
private static T ReadToValue <T>(this SqlCommand cmd) { var readerFlags = GetReaderFlagsAndOpenConnection(cmd, CommandBehavior.SingleRow); using (var dr = cmd.ExecuteReader(readerFlags)) { if (dr.Read()) { if (typeof(T).IsNullableValueType() && dr.IsDBNull(0)) { return(default(T)); } return(SqlDataReaderExtensions.GetValue <T>(dr)); } return(default(T)); } }
public static async Task <IList <T> > ReadAsListAsync <T>(this SqlCommand cmd, IList <T> list) { if (typeof(T).IsSimpleType()) { return(await cmd.ReadToListOfValuesAsync <T>(list)); } var key = SqlDataReaderExtensions.GetAutoCreateObjectFuncKey <T>(cmd.CommandText); var autoMappingFunc = MappingManager.GetAutoCreateObjectFunc <T>(key); var readerFlags = await GetReaderFlagsAndOpenConnectionAsync(cmd, CommandBehavior.SingleResult); using (var dr = await cmd.ExecuteReaderAsync(readerFlags).ConfigureAwait(false)) { list = dr.ReadAsList(list, autoMappingFunc, key); } return(list); }
private static async Task <T> ReadToValueAsync <T>(this SqlCommand cmd) { var readerFlags = await GetReaderFlagsAndOpenConnectionAsync(cmd, CommandBehavior.SingleRow); using (var dr = await cmd.ExecuteReaderAsync(readerFlags).ConfigureAwait(false)) { if (dr.Read()) { if (typeof(T).IsNullableValueType() && dr.IsDBNull(0)) { return(default(T)); } return(SqlDataReaderExtensions.GetValue <T>(dr)); } return(default(T)); } }
private static IEnumerable <T> ReadAsEnumerableObjects <T>(this SqlCommand cmd) { var readerFlags = GetReaderFlagsAndOpenConnection(cmd, CommandBehavior.SingleResult); using (var dr = cmd.ExecuteReader(readerFlags)) { var key = SqlDataReaderExtensions.GetAutoCreateObjectFuncKey <T>(dr); var autoMappingFunc = MappingManager.GetAutoCreateObjectFunc <T>(key); if (autoMappingFunc == null) { autoMappingFunc = SqlDataReaderExtensions.CreateAutoMappingFunc <T>(dr); MappingManager.AddAutoCreateObjectFunc(key, autoMappingFunc); } while (dr.Read()) { yield return(autoMappingFunc(dr)); } } }
public static IEnumerable <T> ReadToEnumerableValues <T>(this SqlCommand cmd) { var type = typeof(T); var isNullableValueType = type.IsNullableValueType(); var readerFlags = GetReaderFlagsAndOpenConnection(cmd, CommandBehavior.SingleResult); using (var dr = cmd.ExecuteReader(readerFlags)) { while (dr.Read()) { if (isNullableValueType) { var underlyingType = Nullable.GetUnderlyingType(type) ?? type; while (dr.Read()) { if (dr.IsDBNull(0)) { yield return(default(T)); } else { yield return(SqlDataReaderExtensions.GetValue <T>(dr, underlyingType)); } } } else { while (dr.Read()) { yield return(SqlDataReaderExtensions.GetValue <T>(dr, type)); } } } } }