Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Método DataSet.CreateDataReader (DataTable[])

 

Retorna um DataTableReader com um conjunto de resultados por DataTable.

Namespace:   System.Data
Assembly:  System.Data (em System.Data.dll)

public DataTableReader CreateDataReader(
	params DataTable[] dataTables
)

Parâmetros

dataTables
Type: System.Data.DataTable[]

Uma matriz de DataTables que fornece a ordem dos conjuntos de resultado a serem retornados no DataTableReader.

Valor Retornado

Type: System.Data.DataTableReader

Um DataTableReader que contém um ou mais conjuntos de resultados, correspondentes às instâncias DataTable contidas no DataSet de origem. Os conjuntos de resultados retornados estão na ordem especificada pelo parâmetro dataTables.

In order to ensure that the order of the result sets within the returned T:System.Data.DataTableReader, if a T:System.Data.DataTable within the T:System.Data.DataSet is empty, it is represented by an empty result set within the returned DataTableReader. Because this overloaded version allows you to supply a list of DataTable instances as parameters, you can specify the order in which the result sets appear within the returned DataTableReader.

This example, a Console application, creates three T:System.Data.DataTable instances and adds each to a T:System.Data.DataSet. The example calls the M:System.Data.DataSet.CreateDataReader(System.Data.DataTable[]) method and displays the contents of the returned T:System.Data.DataTableReader. Note that the order of the result sets in the DataTableReader is controlled by the order of the DataTable instances passed as parameters. The example displays the results in the Console window.

static DataTable customerTable;
static DataTable productTable;
static DataTable emptyTable;

static void Main()
{
    DataSet dataSet = new DataSet();

    // Add some DataTables to the DataSet, including
    // an empty DataTable:
    emptyTable = new DataTable();
    productTable = GetProducts();
    customerTable = GetCustomers();

    dataSet.Tables.Add(customerTable);
    dataSet.Tables.Add(emptyTable);
    dataSet.Tables.Add(productTable);
    TestCreateDataReader(dataSet);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void TestCreateDataReader(DataSet dataSet)
{
    // Given a DataSet, retrieve a DataTableReader
    // allowing access to all the DataSet's data.
    // Even though the dataset contains three DataTables,
    // this code will only display the contents of two of them,
    // because the code has limited the results to the 
    // DataTables stored in the tables array. Because this
    // parameter is declared using the ParamArray keyword, 
    // you could also include a list of DataTable instances 
    // individually, as opposed to supplying an array of 
    // DataTables, as in this example:
    using (DataTableReader reader = 
       dataSet.CreateDataReader(productTable, emptyTable))
    {
        do
        {
            if (!reader.HasRows)
            {
                Console.WriteLine("Empty DataTableReader");
            }
            else
            {
                PrintColumns(reader);
            }
            Console.WriteLine("========================");
        } while (reader.NextResult());
    }
}

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

    // 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;
}

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

    // 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, "Wireless Network Card" });
    table.Rows.Add(new object[] { 2, "Hard Drive" });
    table.Rows.Add(new object[] { 3, "Monitor" });
    table.Rows.Add(new object[] { 4, "CPU" });
    return table;
}

private static void PrintColumns(DataTableReader reader)
{
    // Loop through all the rows in the DataTableReader
    while (reader.Read())
    {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.Write(reader[i] + " ");
        }
        Console.WriteLine();
    }
}

.NET Framework
Disponível desde 2.0
Retornar ao início
Mostrar: