1

I am trying to find an interface which is implemented by SqlDataReader and which exposes HasRows, Read() and NextResult(). I need these property and methods because I need to read multiple resultSets returned by stored procedures (as mentioned here). Currently I am using System.Data.IDataReader which exposes Read() and NextResult() but not HasRows property.

I need an interface so that the coupling will be lose and for the sake of code testability.

Any help on such an interface? Or I'll need to write an abstraction layer of interface from scratch?

Community
  • 1
  • 1
upInCloud
  • 979
  • 2
  • 10
  • 29
  • 2
    There is no such interface. You should write it yourself. – Niyoko Oct 29 '16 at 08:50
  • Why dont you implement IDataReader for Read() and NextResult() in your own Interface and then implement your own custom interface for hasRows this will reduce your scratchwork – Zain Ul Abidin Oct 29 '16 at 09:32
  • `IDataReader` itself is fine because [`IDataReader.NextResult()`](https://msdn.microsoft.com/en-us/library/system.data.idatareader.nextresult.aspx) returns a `bool` so you can do `do { ... } while (reader.NextResult());` See for instance [this answer](https://stackoverflow.com/questions/38953677/write-sqldatareader-to-immediate-window-c-sharp/39757521#39757521). – dbc Oct 31 '16 at 06:54

1 Answers1

3

HasRows is not needed for anything (usually it's being used redundantly because people don't know any better). The standard pattern is:

while (reader.Read()) ...

So this obviates the need for the interface you are looking for.

usr
  • 168,620
  • 35
  • 240
  • 369