Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I keep a SqlDataReader "alive" after closing connection?

Is there any way to access a SqlDataReader after the connection is closed?

Or is there any objects equivalent to SqlDataReader that I can store the reader into them and process on the objects later?

I'm receiving a pivot dataset from the server, so I can't use normal classes to process this kind of data, my model looks like this :

public class OneToNinetyNine
{
    public List<Cities> listCities;
    public string CityID;
    public DateTime DateFrom;
    public DateTime DateTo;
    // this is the reader that I attempt to pass to the views 
    public SqlDataReader SqlReader; 
}
like image 613
NeedAnswers Avatar asked Dec 26 '22 02:12

NeedAnswers


1 Answers

You can't use the DataReader after the connection is closed, as it needs to use the connection to retrieve the data from the data source.

You need to read the data into a DataSet, or DataTable using the Load method, then you can close the connection.

like image 140
Ben Avatar answered Feb 22 '23 23:02

Ben