DataTableReader.HasRows Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient une valeur qui indique si les DataTableReader lignes contiennent une ou plusieurs lignes.
public:
virtual property bool HasRows { bool get(); };
public override bool HasRows { get; }
member this.HasRows : bool
Public Overrides ReadOnly Property HasRows As Boolean
Valeur de propriété
true si la DataTableReader valeur contient une ou plusieurs lignes ; sinon false.
Exceptions
Une tentative a été effectuée pour récupérer des informations sur une fermeture DataTableReader.
Exemples
L’exemple suivant remplit deux DataTable instances avec des données. La première DataTable contient une ligne, et la seconde ne contient aucune ligne. L’exemple crée ensuite un DataTableReader qui contient les deux DataTable objets et appelle la méthode PrintData pour afficher le contenu de chacune d’elles, en vérifiant la valeur de la HasRows propriété de chacune d’elles avant d’appeler 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
Remarques
La HasRows propriété retourne des informations sur le jeu de résultats actuel. Si le DataTableReader jeu de résultats contient plusieurs jeux de résultats, vous pouvez examiner la valeur de la HasRows propriété immédiatement après avoir appelé la NextResult méthode pour déterminer si le nouveau jeu de résultats contient des lignes.
Utilisez la HasRows propriété pour éviter l’exigence d’appeler la méthode de la ReadDataTableReader méthode s’il n’existe aucune ligne dans le jeu de résultats actuel.