Which would be quicker.
1) Looping a datareader and creating a custom rows and columns based populated datatable
2) Or creating a dataAdapter object and just (.Fill)ing a datatable.
Does the performance of a datareader still hold true upon dynamic creation of a datatable?
As with many questions like this the answer is: depends.
If you don't know the structure of your data up front and are creating TableAdapters on the fly, then the dynamic DataTable would be more efficient. There is a good deal of code generation involved in creating a TableAdapter.
However, if you know the structure of your data up front then the question becomes, How much functionality do I need?
If you need a full CRUD implementation then there are some efficiencies gained by using a TableAdapter rather than writing all that CRUD code yourself. Also, the TableAdapter implementation is OK (not great). If you need something more efficient then you may be better off using nHibernate or some other ORM.
If you don't need a full CRUD implementation (i.e., this is a read-only solution) and know your data structure up front, then you'll have to test the efficiency of a TableAdapter read-only implementation against a dynamically generated DataTable. If I were a betting man I'd put my money on the TableAdapter implementation since you bind data once and read it multiple times.
Going by
DataReader
'sRead
which is a forward-only, one-row-at-a-time approach, which reads data sequentially so that you get records as soon as they are read when being connected, will be the best for memory and performance.That said, between the two approaches, I find
IDataAdapter.Fill
much faster thanDataTable.Load
. Of course that depends on implementations.. Here is a benchmark between the two which I posted here:The second approach always outperformed the first.
Read1
looks better on eyes, but data adapter performs better (not to confuse that one db outperformed the other, the queries were all different). The difference between the two depended on query though. The reason could be thatLoad
requires various constraints to be checked row by row from the documentation when adding rows (its a method onDataTable
) whileFill
is on DataAdapters which were designed just for that - fast creation of DataTables.I cant speak to filling a datatable per se but using a datareader is the most efficient reading method.
Your option #1 would be slower. However, there's a better way to convert a datareader to a datatable than adding custom rows by hand:
I can't comment on the difference between this and using
.Fill()
.The DataAdapter uses a DataReader under the hood so your experience would likely be the same.
The benefit of the DataAdapter is you cut out a lot of code that would need maintenance.
This debate is a bit of a religious issue so definitely look around and decide what works best for your situation:
The datareader is faster. And if you are using 2.0+ you probablt don't even have to use a datatable. You can use a generic list of your object.