由DbDataReader得到强类型集合

public List ToList(DbDataReader dr) where T : class, new()
{
    var result = new List();
    var properties = typeof(T).GetProperties().ToList();
    while (dr.Read())
    {
        var obj = new T();

        foreach (var property in properties)
        {
            try
            {
                //Oracle字段为大写
                var id = dr.GetOrdinal(property.Name.ToUpper());
                if (!dr.IsDBNull(id))
                {
                    if (dr.GetValue(id) != DBNull.Value)
                    {
                        property.SetValue(obj, dr.GetValue(id));
                    }
                }
            }
            catch
            {
            }
        }

        result.Add(obj);
    }
    return result;
}

你可能感兴趣的:(.NET)