Neeraj Dubey Neeraj Dubey - 3 months ago 35
C# Question

When to use IDataReader and when to use DataReader?

I know that

IDataReader
is the interface and
DataReader
is the concrete type but I still don't know when to use each one. I need to iterate through the data which is possible using both
Datareader
and
IDataReader
. Is there a better way to decide when to use the interface or the concrete type?

Answer

SqlDataReader and all other data providers implement IDataReader. If you think that you may change the provider from sql to oracle or something else in future then use IDataReader. You will have the luxury of changing that without changing your code where you have used IDataReader. Else you can use SqlDataReader. But if you use IDataReader it will be a decoupled design and is recommeneded.