DataTableReader.HasRows Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a value that indicates whether the DataTableReader contains one or more rows.
public:
virtual property bool HasRows { bool get(); };
public override bool HasRows { get; }
member this.HasRows : bool
Public Overrides ReadOnly Property HasRows As Boolean
true
if the DataTableReader contains one or more rows; otherwise false
.
An attempt was made to retrieve information about a closed DataTableReader.
The following example fills two DataTable instances with data. The first DataTable contains one row, and the second contains no rows. The example then creates a DataTableReader that contains both DataTable objects, and calls the PrintData method to display the contents of each, checking the value of the HasRows property of each before it makes the call to 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
The HasRows
property returns information about the current result set. If the DataTableReader contains multiple result sets, you can examine the value of the HasRows
property immediately after you call the NextResult method in order to determine whether the new result set contains rows.
Use the HasRows
property to avoid the requirement to call the Read method of the DataTableReader if there are no rows within the current result set.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: