DataTableReader.HasRows Propiedad

Definición

Obtiene un valor que indica si DataTableReader contiene una o varias filas.

public:
 virtual property bool HasRows { bool get(); };
public override bool HasRows { get; }
member this.HasRows : bool
Public Overrides ReadOnly Property HasRows As Boolean

Valor de propiedad

true si DataTableReader contiene una o varias filas; en caso contrario, false.

Excepciones

Se ha intentado recuperar información sobre un objeto DataTableReader cerrado.

Ejemplos

En el ejemplo siguiente se rellenan dos DataTable instancias con datos. La primera DataTable contiene una fila y la segunda no contiene filas. A continuación, el ejemplo crea un DataTableReader objeto que contiene ambos DataTable objetos y llama al método PrintData para mostrar el contenido de cada uno, comprobando el valor de la HasRows propiedad de cada uno antes de realizar la llamada a PrintData.

private static void TestHasRows()
{
    DataTable customerTable = GetCustomers();
    DataTable productTable = GetProducts();

    using (DataTableReader reader = new DataTableReader(
               new DataTable[] { customerTable, productTable }))
    {
        do
        {
            if (reader.HasRows)
            {
                PrintData(reader);
            }
        } while (reader.NextResult());
    }

    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();
}

private static void PrintData(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();
    }
}

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" });
    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 };
    return table;
}
Private Sub TestHasRows()
   'Retrieve one row from the Store table:
   Dim customerTable As DataTable = GetCustomers()
   Dim productsTable As DataTable = GetProducts()

   Using reader As New DataTableReader( _
      New DataTable() {customerTable, productsTable})

      Do
         If reader.HasRows Then
            PrintData(reader)
         End If
      Loop While reader.NextResult()
   End Using

   Console.WriteLine("Press Enter to finish.")
   Console.ReadLine()
End Sub

Private Sub PrintData( _
   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("{0} ", reader(i))
      Next
      Console.WriteLine()
   Loop
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"})
   Return table
End Function

Private Function GetProducts() As DataTable
   ' Create sample Products 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}

   Return table
End Function

Comentarios

La HasRows propiedad devuelve información sobre el conjunto de resultados actual. DataTableReader Si contiene varios conjuntos de resultados, puede examinar el valor de la HasRows propiedad inmediatamente después de llamar al NextResult método para determinar si el nuevo conjunto de resultados contiene filas.

Utilice la HasRows propiedad para evitar el requisito de llamar al Read método de DataTableReader si no hay filas dentro del conjunto de resultados actual.

Se aplica a

Consulte también