Neeraj Dubey Neeraj Dubey - 1 year ago 158
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download