I would like to retrieve all System.Data.IDataRecord
s from a SqlDataReader
. With LINQ, this is simple, but I'd like to be able to dispose of all SQL objects and keep the data intact. Below is what I have:
private static List<System.Data.IDataRecord> getRecords()
{
List<System.Data.IDataRecord> records;
//Make a SqlConnection and SqlCommand.
using (SqlDataReader reader = command.ExecuteReader())
{
records = reader.Cast<System.Data.IDataRecord>().ToList();
}
//Dispose of SqlCommand and SqlConnection.
return records;
}
But here is my question: When getRecords()
is finished, I have my data, and I have asked for all SQL objects to be disposed. But am I somehow hanging onto data which will prevent proper resource disposal? Or will my data eventually become invalid once the garbage collector does its work? I think the answer is no, but I am trying to find something definitive. I believe Dispose()
will only remove unmanaged resources, and since I placed the data in my List, I would think it is safe. Thanks.
This will work fine.
IDataRecord
instances are not affected by SqlDataReader.Dispose
in any way. They contain their own full copy of the data and schema info, completely separate from the reader.