DataTableReader.HasRows 속성

정의

DataTableReader에 하나 이상의 행이 있는지 여부를 나타내는 값을 가져옵니다.

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

속성 값

DataTableReader에 행이 하나 이상 포함되어 있으면 true이고, 그렇지 않으면 false입니다.

예외

닫힌 DataTableReader에 대한 정보를 검색하려고 한 경우

예제

다음 예제에서는 두 개의 채웁니다 DataTable 데이터와 관련 된 인스턴스. 첫 번째 DataTable 하나의 행을 포함 하 고 두 번째 행이 없는 포함 합니다. 만듭니다는 DataTableReader 모두 포함 된 DataTable 개체의 값을 확인 하는 각각의 내용을 표시 하는 PrintData 메서드를 호출 하는 HasRows 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

설명

HasRows 속성 현재 결과 집합에 대 한 정보를 반환 합니다. 경우는 DataTableReader 여러 결과가 포함 집합의 값을 검사할 수 있습니다 합니다 HasRows 호출한 후에 즉시 속성은 NextResult 새 결과 집합에 행이 포함 되어 있는지 확인 하기 위해 메서드.

사용 하 여는 HasRows 호출을 피하기 위해 속성을 Read 메서드는 DataTableReader 현재 결과 집합 내의 행이 없는 경우.

적용 대상

추가 정보