DataRow and DataReader

  • Thread starter Thread starter MattC
  • Start date Start date
M

MattC

Hi,

Given that a data item in both a DataRow and DataReaders IDataRecord can be
accessed using something like

dr["ColumnName"].

I'd like to create method that accepts either a DataRow or IDataRecord.
Anyone know if this is possible? Is there an interface that exposes an
indexer that I can cast them to?

TIA

MattC
 
MattC,

You can just overload the method to accept a data row or an IDataRecord
(you might also want to consider a DataRowView, because you might be working
with those as well).

What I think you want to do is actually create a method that takes the
actual value you want to work with, and then have overloads which will
extract the value from the three data structures. There isn't a common
interface/base class that they all share that exposes the indexer, so you
will have to write that logic yourself.

You ^could^ use reflection to get the indexer and then get the value,
but to be quite honest, it really isn't worth the hassle.
 
"There isn't a common interface/base class that they all share that exposes
the indexer."

Ok, cool that's what I was after..thanks


Nicholas Paldino said:
MattC,

You can just overload the method to accept a data row or an IDataRecord
(you might also want to consider a DataRowView, because you might be
working with those as well).

What I think you want to do is actually create a method that takes the
actual value you want to work with, and then have overloads which will
extract the value from the three data structures. There isn't a common
interface/base class that they all share that exposes the indexer, so you
will have to write that logic yourself.

You ^could^ use reflection to get the indexer and then get the value,
but to be quite honest, it really isn't worth the hassle.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

MattC said:
Hi,

Given that a data item in both a DataRow and DataReaders IDataRecord can
be accessed using something like

dr["ColumnName"].

I'd like to create method that accepts either a DataRow or IDataRecord.
Anyone know if this is possible? Is there an interface that exposes an
indexer that I can cast them to?

TIA

MattC
 
Hi,


MattC said:
Hi,

Given that a data item in both a DataRow and DataReaders IDataRecord can
be accessed using something like

dr["ColumnName"].

I'd like to create method that accepts either a DataRow or IDataRecord.
Anyone know if this is possible? Is there an interface that exposes an
indexer that I can cast them to?

You can create two overloaded methods each one accepting the correct type,
then you can use a third method that do the work with the value you read.

Also you could read all the Datareader's data into a dataset and then use
one method.
 
Back
Top