I'm reading some fields with different types from an SqlDataReader
based on the column ID. I tried to write a generic function to get the fields in a List
.
private static List<T> GetSqlFields<T>(SqlDataReader reader, int columnId)
{
var fields = new List<T>();
while (reader.Read())
{
T f = reader.Get... // Problem is here
fields.Add(f);
}
return fields;
}
Problem is I only see methods GetString
, GetInt32
, etc.. Is there a way to get the fields based on T
? Thank you.
I'm doing this to be able to write:
int fooColumnId = reader.GetOrdinal("Foo");
int barColumnId = reader.GetOrdinal("Baar");
List<string> foos = GetSqlFields<string>(reader, fooColumnId);
List<int> baars = GetSqlFields<int>(reader, barColumnId);