예제 #1
0
        public List <T> ReadSQLBaseTypesUnsafe <T>(IDataReader Reader, int limit = -1)
        {
            List <T> Entries          = new List <T>();
            int      CompatableColumn = -1;
            bool     CastRequired     = false;
            bool     IsNumeric        = SQLTypeHelper.NumericType(typeof(T));

            for (int i = 0; i < Reader.FieldCount; i++)
            {
                Type SQLType = Reader.GetFieldType(i);
                if (typeof(T).IsAssignableFrom(SQLType))
                {
                    CompatableColumn = i;
                }
                else if (SQLTypeHelper.CanCastEquivilant(SQLType, typeof(T)) || (IsNumeric && SQLTypeHelper.NumericType(SQLType)))
                {
                    CompatableColumn = i;
                    CastRequired     = true;
                }
            }
            if (CompatableColumn == -1)
            {
                throw new SQLIncompatableTypeException();
            }
            bool checkLimit = limit != -1;
            int  count      = 0;

            while (Reader.Read())
            {
                var value = Reader.GetValue(CompatableColumn);
                count++;
                if (value is DBNull)
                {
                    Entries.Add(default(T));
                }
                else if (CastRequired)
                {
                    Entries.Add((T)Convert.ChangeType(value, typeof(T)));
                }
                else
                {
                    Entries.Add((T)value);
                }
                if (checkLimit && count >= limit)
                {
                    break;
                }
            }
            return(Entries);
        }
예제 #2
0
        public async Task <List <T> > ReadSQLBaseTypesUnsafeAsync <T>(DbDataReader Reader, int limit = -1)
        {
            List <T> Entries          = new List <T>();
            int      CompatableColumn = -1;
            bool     IsNumeric        = SQLTypeHelper.NumericType(typeof(T));

            for (int i = 0; i < Reader.FieldCount; i++)
            {
                Type SQLType = Reader.GetFieldType(i);
                if (typeof(T).IsAssignableFrom(SQLType))
                {
                    CompatableColumn = i;
                }
                else if (SQLTypeHelper.CanCastEquivilant(SQLType, typeof(T)) || (IsNumeric && SQLTypeHelper.NumericType(SQLType)))
                {
                    CompatableColumn = i;
                }
            }
            if (CompatableColumn == -1)
            {
                throw new SQLIncompatableTypeException();
            }

            bool checkLimit = limit != -1;
            int  count      = 0;

            while (await Reader.ReadAsync())
            {
                count++;
                Entries.Add(await Reader.GetFieldValueAsync <T>(CompatableColumn));
                if (checkLimit && count >= limit)
                {
                    break;
                }
            }
            return(Entries);
        }