Neeraj Dubey Neeraj Dubey - 2 months ago 17x
C# Question

When to use IDataReader and when to use DataReader?

I know that

is the interface and
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
. Is there a better way to decide when to use the interface or the concrete type?


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.