Search notes:

System.Data.Common.DbDataReader (class)

An instance of System.Data.Common.DbDataReader is returned by the ExecuteReader() method of System.Data.Common.DbCommand.
Such a DbDataReader is used if the DbCommand represents an SQL statement that returns values, typically a select statements. The returned DbDataReader implements System.Collection.IEnumerable, so the returned records from the SQL statement can be iterated over with a foreachin statement.
An interesting method of the DbDataReader type is GetSchemaTable(): it returns the metadata about the columns of the queries resultset, notably column names and their data types.

Methods and properties

DbDataReader is an abstract class, so these (or at least some of these) methods and properties must be overwritten in derived classes.
Close()
CloseAsync()
Depth
Dispose()
DisposeAsync()
FieldCount Number of columns in the result set.
GetTYPE(pos) Returns the value of pos as TYPE. TYPE is for example Byte, Char, DataTime, Decimal, Double, Int16, Int32, Int64, String etc.
GetBytes()
GetChars()
GetColumnSchemaAsync()
GetData() Returns the nested data reader (also a DbDataReader object). Compare with GetDbDataReader()
GetDataTypeName(pos) Returns the type of pos as string. Compare with GetFieldType()
GetDbDataReader() Returns a DbDataReader. Compare with GetData()
GetEnumerator() Returns a System.Collections.IEnumerator.
GetFieldType(pos) Returns a System.Type that corresponds to the type of pos. Compare with GetDataTypeName()
GetFieldValue<T>(pos) Returns the value of pos as type T
GetFieldValueAsync()
GetName(pos) Name of column pos
GetOrdinal(name) Gets the column number that corresponds to the column whose name is name.
GetProviderSpecificFieldType()
GetProviderSpecificValue()
GetProviderSpecificValues()
GetSchemaTable()
GetSchemaTableAsync()
GetStream() Returns a System.IO.Stream
GetTextReader() Returns a System.IO.TextReader
GetValue(pos) Gets the value of pos as a System.Object
GetValues()
HasRows
IsClosed
IsDBNull(pos) true if the corresponding value is an (SQL) null value.
IsDBNullAsync()
Item[]
NextResult()
NextResultAsync()
Read()
ReadAsync()
RecordsAffected
VisibleFieldCount

Example: iterating over SQL select statement result set

The following simple C# program tries to demonstrate how a DbDataReader instance can be used to iterate over the result set of an SQL statement.
For simplicity reasons, this example uses a CSV file and the Microsoft OLE DB Provider for Microsoft Access database engine to select from the CSV file. In the real world, of course, a more serious database would be used :)
using System;
using System.Data.Common;
using System.Data.OleDb;
using System.IO;

class Prg {

   static void Main () {

      string csvFilePath = Directory.GetCurrentDirectory();

      string provider =
         // "Microsoft.Jet.OLEDB.4.0"
            "Microsoft.ACE.OLEDB.12.0";

      string connectionString           =
          $"Provider={provider};"       + 
          $"Data Source={csvFilePath};" +
          $"Extended Properties='text';";

      using OleDbConnection connection = new OleDbConnection(connectionString);
      connection.Open();

      DbCommand command = new OleDbCommand("select id, num, txt from data.csv", connection);
//
//       Of course, the constructor returns an OleDbCommand object. Thus, the
//       following line would probably be more correct. But because this
//       program serves at demonstrating the basic functionality of ExecuteReader() and
//       Read(), I have not done so.
//
//    OleDbCommand command = new OleDbCommand("select id, num, txt from data.csv", connection);


      DbDataReader reader = command.ExecuteReader();
//
//       With an OleDbCommand, the ExecuteReader method returns
//       a DbDataReader object:
//
//    OleDbDataReader reader = command.ExecuteReader();

      while (reader.Read()) {
         Console.WriteLine(
            String.Format(" {0,2} | {1,5:F1} | {2}",
               reader.GetInt32 (0),
               reader.GetDouble(1),
               reader.GetString(2)
         ));
      }
   }
}
Github repository .NET-API, path: /System/Data/Common/iterate.cs
For completeness' sake, this is the CSV file:
id,num,txt
1,5,five
2,99.9,ninty-nine dot nine
3,-42,minus forty-two
Github repository .NET-API, path: /System/Data/Common/data.csv

See also

System.Data.OleDb.OleDbDataReader

Index