views:

149

answers:

5

I want to create a simple class that is similiar to a datatable, but without the overhead.

So loading the object with a sqldatareader, and then return this custom datatable-like object that will give me access to the rows and columns like:

myObject[rowID]["columnname"]

How would you go about creating such an object?

I don't want any built in methods/behavoir for this object except for accessing the rows and columns of the data.

Update:

I don't want a datable, I want something much leaner (plus I want to learn how to create such an object).

A: 

Isn't this a DataSet/DataTable? Maybe I didn't get the question. Also, what is the programming language?

Khash
+3  A: 

This type of structure can be easily created with a type signature of:

List<Dictionary<string, object>>

This will allow access as you specify and should be pretty easy to populate.

You can always create an object that inherits from List < Dictionary < string, object > > and implements a constructor that takes a SqlDataReader. This constructor should create a enw dictionary for each row, and insert a new entry into the dictionary for each column, using the column name as the key.

Jack Ryan
do you know the default capicty for teh dictionary?
Blankman
This is NOT lightwait, as a instance of the Dictionary is created for each row.
Ian Ringrose
+1  A: 

I think you're missing something about how .Net works. The extra overhead involved in a DataTable is not significant. Can you point to a specific performance problem in existing code that you believe is caused by a datatable? Perhaps we can help correct that in a more elegant way.

Joel Coehoorn
From what I understand a datatable is a database in object, so that must come with some overhead obviously which I don't need. A custom implementation seems simple enough so why not?
Blankman
No: a datatable is NOT an entire database in an object.
Joel Coehoorn
It does pull the entire query results into memory at once, though. If that's where your problem is, what you want instead is a DataReader. A DataReader will only load one row into memory at a time, and is VERY fast.
Joel Coehoorn
A datatable or dataset has allot of built in functionality, so the payload of the object will be much more than a streamlined version that I wanted. i.e. you can perform SELECTS on a datatable, joins, have primary keys etc.
Blankman
The 'payload' your talking about is hidden in the code section, and loaded as part of the .Net framework. You MUST pay that price whether or not you choose to use a datatable.
Joel Coehoorn
A: 

Perhaps the specific thing you're asking about is how to use the convenient ["whatever"] indexing syntax in your own table object.

If so, I suggest you refer to this MSDN page on indexers.

mquander
A: 

Dictionary<int,object[]> would be better than List<Dictionary<string, object>>. You don't really need a dictionary for each row, since column names are the same for all rows. And if you want to have it lightweight, you should use column indexes instead of names.

So if you have a column "Name" that is a 3rd column, to get its value "Name" from a row ID 10, the code would be:

object val = table[10][2];

Another option is SortedList<int,object[]>... depending on the way you access the data (forward only or random access).

You could also use MultiDictionary<int,object> from PowerCollections.

From the memory usage perspective, I think the best option would be to use a single dimension array with some slack capacity. So after each, say 100, rows, you would create a new array, copy the old contents to it and leave 100 empty rows at the end. But you would have to keep some sort of an index when you delete a row, so that it is marked as deleted without resizing the array.

Igor Brejc