I'm trying to fetch records form Azure database using Ado.net I used SqlDataReader class for that. Even though the data fetch is successful, I don't know how to convert it into a generic list.
protected List<T> GetList<T>()
{
try
{
using (var query = ExecuteReader())
{
// What Goes Here ?
}
}
finally
{
if (_sqlCommand.Connection.State == ConnectionState.Open)
{
_sqlCommand.Connection.Close();
}
}
}
ExecuteReader method,
protected SqlDataReader ExecuteReader()
{
if (_sqlCommand.Connection.State != ConnectionState.Open)
{
_sqlCommand.Connection.Open();
}
return _sqlCommand.ExecuteReader();
}
The Data Model,
public class Student
{
[EntityKey]
public int StudentId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Major { get; set; }
}
NOTE: I would like to know if there any other easy ways as well
SqlDataReaderisn't a container, it's a cursor used to load data. You don't convert it to a List, you use it to load the rows, construct the objects and add them to a list. Or use an ORM or Dapper and have them do this for you