Hi..
I want to return list which will store the sqldatareader
value. how can i do this..
Hi..
I want to return list which will store the sqldatareader
value. how can i do this..
using (var reader = cmd.ExecuteReader())
{
var result = new List<Foo>();
while (reader.Read())
{
var foo = new Foo
{
Prop1 = reader.GetInt32(0),
Prop2 = reader.GetString(1),
}
result.Add(foo);
}
}
Also check this previously asked qestion :
http://stackoverflow.com/questions/1464883/how-can-i-easily-convert-datareader-to-listt
Check this for generic type :
public ConvertToList<T>(SqlDataReader sqldr, int index)
{
List<T> list = new List<T>();
while (sqldr.Read()) {
list.Add((T)sqldr.GetValue(index)); index++;
}
return list;
}