Edit

Share via


Creating a DataReader

The DataTable and DataSet classes have a CreateDataReader method that returns the contents of the DataTable or the contents of the DataSet object's Tables collection as one or more read-only, forward-only result sets.

Example

The following console application creates a DataTable instance. The example then passes the filled DataTable to a procedure that calls the CreateDataReader method, which iterates through the results contained within the DataTableReader.

static void Main()
{
    TestCreateDataReader(GetCustomers());
    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

static void TestCreateDataReader(DataTable dt)
{
    // Given a DataTable, retrieve a DataTableReader
    // allowing access to all the tables' data:
    using (DataTableReader reader = dt.CreateDataReader())
    {
        do
        {
            if (!reader.HasRows)
            {
                Console.WriteLine("Empty DataTableReader");
            }
            else
            {
                PrintColumns(reader);
            }
            Console.WriteLine("========================");
        } while (reader.NextResult());
    }
}

static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    return table;
}

static void PrintColumns(DataTableReader reader)
{
    // Loop through all the rows in the DataTableReader
    while (reader.Read())
    {
        for (var i = 0; i < reader.FieldCount; i++)
        {
            Console.Write(reader[i] + " ");
        }
        Console.WriteLine();
    }
}
Sub Main()
    TestCreateDataReader(GetCustomers())
    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
End Sub

Private Sub TestCreateDataReader(ByVal dt As DataTable)
    ' Given a DataTable, retrieve a DataTableReader
    ' allowing access to all the tables's data:
    Using reader As DataTableReader = dt.CreateDataReader()
        Do
            If Not reader.HasRows Then
                Console.WriteLine("Empty DataTableReader")
            Else
                PrintColumns(reader)
            End If
            Console.WriteLine("========================")
        Loop While reader.NextResult()
    End Using
End Sub

Private Function GetCustomers() As DataTable
    ' Create sample Customers table, in order
    ' to demonstrate the behavior of the DataTableReader.
    Dim table As New DataTable

    ' Create two columns, ID and Name.
    Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
    table.Columns.Add("Name", GetType(String))

    ' Set the ID column as the primary key column.
    table.PrimaryKey = New DataColumn() {idColumn}

    table.Rows.Add(New Object() {1, "Mary"})
    table.Rows.Add(New Object() {2, "Andy"})
    table.Rows.Add(New Object() {3, "Peter"})
    table.Rows.Add(New Object() {4, "Russ"})
    Return table
End Function

Private Sub PrintColumns( _
   ByVal reader As DataTableReader)

    ' Loop through all the rows in the DataTableReader.
    Do While reader.Read()
        For i As Integer = 0 To reader.FieldCount - 1
            Console.Write(reader(i).ToString() & " ")
        Next
        Console.WriteLine()
    Loop
End Sub

The example displays the following output in the console window:

1 Mary  
2 Andy  
3 Peter  
4 Russ  

See also