DataTableReader.HasRows プロパティ

定義

DataTableReader に 1 行以上の行が格納されているかどうかを示す値を取得します。

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

プロパティ値

1 行以上の行が DataTableReader に含まれている場合は true。それ以外の場合は false

例外

閉じられた DataTableReader に関する情報を取得しようとしました。

次の例では、2 つの DataTable インスタンスにデータを入力します。 1 つ目 DataTable には 1 つの行が含まれており、2 番目の行には行が含まれています。 次に、両方DataTableのオブジェクトを含む をDataTableReader作成し、PrintData メソッドを呼び出してそれぞれの内容を表示し、PrintData を呼び出す前にそれぞれの プロパティのHasRows値を確認します。

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

注釈

プロパティは HasRows 、現在の結果セットに関する情報を返します。 に DataTableReader 複数の結果セットが含まれている場合は、 メソッドを呼び出した直後に プロパティの HasRows 値を NextResult 調べて、新しい結果セットに行が含まれているかどうかを判断できます。

現在のHasRows結果セット内に行がない場合に の DataTableReader メソッドをRead呼び出す要件を回避するには、 プロパティを使用します。

適用対象

こちらもご覧ください