Question

I am using SqlDataReader to read data from SQL Server. However sometimes I want to cache my data in memory as a read-only lightweight connection-free object. I couldn't find any options for me in the BCL. Do I need to write my own implementation of such container, or are any available?

What alternatives do I have for storing data from database in lightweight form? For me DataTable is not a candidate at all.

EDIT:

When I know what data I am selecting, I can easily use LINQ to transform my reader into List<T> or something. But instead I would like to be able to cache data which I don't know the structure of.

Was it helpful?

Solution

List<object[]>

seems about as lightweight as you can get.

And if you want more functionality that it offers (such as column names, databinding support, sorting, filtering), then you can derive / encapsulate and add that functionality.

Your requirement's rather vague. What does lightweight mean? What is it about DataTable that is unsuitable? What functionality that's provided by DataTable do you need?

OTHER TIPS

Create your own data structure.

The BCL should not be all things to all people. It consists of building blocks. The BCL contains the building blocks you need to create your own data structure.

If the BCL contained solutions to every problem it would be a bloated mess. Not every piece of functionality should be available "off the shelf", otherwise there would be no value left for you as a programmer to add.

You seem to have fairly clear requirements that are different from "normal" usage patterns. Between DataSets and ORMs, the large majority of projects get along just fine without the functionality your requirements are calling for.

You can use List<Dictionary<String, Object>>, where Dictionary key is column name and value is cell value.

If you have many rows, you can separate column names into List<String> and values List<List<Object>> (or Array).

The best choice would be to have a type that represents your data columns, and then just construct an instance of that class for each row, fill its properties with the values from the reader, and store the instances in a List<YourClass> structure.

Something like:

using(SqlDataReader rdr = sqlCommand.ExecuteReader())
{
    List<YourClass> resultList = new List<YourClass>();

    while(rdr.Read())
    {
         YourClass work = new YourClass();

         // set the properties        
         work.Property1 = rdr["Column1"].Value;
         .....
         work.PropertyN = rdr["ColumnN"].Value;

         resultList.Add(work);
    } 
} 

That's basically at its core what any ORM (object-relational mapper) does - read the data from the database using the fastest possible way (DataReader) and construct .NET objects from it.

Marc

Use this:

List<string> Variable1 = new List<string>();
//Do something with the generic

string[] MyArray = Variable1.ToArray();

This is the must lightweight and efficient obj to save to memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top