Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert/cast SqlDataReader to IDatareader

What is the easiest way to cast a SqlDataReader to IDatareader.

Or is it easier / possible to convert a List<> object to a IDataReader

like image 293
TheAlbear Avatar asked Nov 19 '10 14:11

TheAlbear


2 Answers

What is the easiest way to convert a SqlDataReader to IDatareader

Since SqlDataReader implements IDataReader, this is just an implicit cast:

SqlDataReader typedReader = ...
IDataReader untypedReader = typedReader;
like image 106
Marc Gravell Avatar answered Sep 22 '22 01:09

Marc Gravell


What is the easiest way to convert a SqlDataReader to IDatareader

You can not. A SqlDataReader CLASS is not an IDataReader interfadce.

For casting (assign to variable) use

IDataReader untypedReader = typedReader; 

BUT: this is NOT a conversion. It is a cast. A conversion to an inteerface is per defintiion not possible as interfaces can not be instantiated.

like image 22
TomTom Avatar answered Sep 25 '22 01:09

TomTom