ExtensionMethod.NET Home of 841 C#, Visual Basic, F# and Javascript extension methods

IDataReader.AsSeq

Exposes a reader's current result set as seq<IDataRecord>. Reader is closed when sequence is fully enumerated. Bonus method: turns an IDataReader with multiple result sets into a seq<seq<IDataRecord>>.

Source

namespace Foo

[<AutoOpen>]
module FsDataEx =

    type System.Data.IDataReader with

        /// Exposes a reader's current result set as seq<IDataRecord>.
        /// Reader is closed when sequence is fully enumerated.
        member this.AsSeq =
seq { use reader = this
      while reader.Read() do 
          yield reader :> IDataRecord }

        /// Exposes all result sets in a reader as seq<seq<IDataRecord>>.
        /// Reader is closed when sequence is fully enumerated.
        member this.AsMultiSeq =
let rowSeq (reader:IDataReader)  =
    seq { while reader.Read() do yield reader :> IDataRecord }
seq {
    use reader = this
    yield rowSeq reader
    while reader.NextResult() do
        yield rowSeq reader
}

Example

for row in reader.AsSeq do
// stuff with row

Author: Joel Mueller

Submitted on: 29 okt 2010

Language: F#

Type: System.Data.IDataReader

Views: 3215