Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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));
            }
        }
        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));
            }
        }
Пример #5
0
        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));
                        }
                    }
                }
            }
        }