DataRowCollection.Find Méthode
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 DataRow valeur à l’aide de la valeur spécifiée PrimaryKey .
Surcharges
| Nom | Description |
|---|---|
| Find(Object[]) |
Obtient la ligne qui contient les valeurs de clé primaire spécifiées. |
| Find(Object) |
Obtient la ligne spécifiée par la valeur de clé primaire. |
Remarques
Les performances doivent être une opération O(log n).
Find(Object[])
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
Obtient la ligne qui contient les valeurs de clé primaire spécifiées.
public:
System::Data::DataRow ^ Find(cli::array <System::Object ^> ^ keys);
public System.Data.DataRow? Find(object?[] keys);
public System.Data.DataRow Find(object[] keys);
member this.Find : obj[] -> System.Data.DataRow
Public Function Find (keys As Object()) As DataRow
Paramètres
- keys
- Object[]
Tableau de valeurs de clé primaire à rechercher. Le type du tableau est Object.
Retours
Objet DataRow qui contient les valeurs de clé primaire spécifiées ; sinon, une valeur Null si la valeur de clé primaire n’existe pas dans le DataRowCollection.
Exceptions
Aucune ligne ne correspond à cette valeur d’index.
La table n’a pas de clé primaire.
Exemples
L’exemple suivant utilise les valeurs d’un tableau pour rechercher une ligne spécifique dans une collection d’objets DataRow . La méthode part du principe qu’il DataTable existe avec trois colonnes clés primaires. Après avoir créé un tableau des valeurs, le code utilise la Find méthode avec le tableau pour obtenir l’objet particulier souhaité.
private void FindInMultiPKey(DataTable table)
{
// Create an array for the key values to find.
object[]findTheseVals = new object[3];
// Set the values of the keys to find.
findTheseVals[0] = "John";
findTheseVals[1] = "Smith";
findTheseVals[2] = "5 Main St.";
DataRow foundRow = table.Rows.Find(findTheseVals);
// Display column 1 of the found row.
if(foundRow != null)
Console.WriteLine(foundRow[1]);
}
Private Sub FindInMultiPKey(ByVal table As DataTable)
' Create an array for the key values to find.
Dim findTheseVals(2) As Object
' Set the values of the keys to find.
findTheseVals(0) = "John"
findTheseVals(1) = "Smith"
findTheseVals(2) = "5 Main St."
Dim foundRow As DataRow = table.Rows.Find(findTheseVals)
' Display column 1 of the found row.
If Not (foundRow Is Nothing) Then
Console.WriteLine(foundRow(1).ToString())
End If
End Sub
Remarques
Pour utiliser la Find méthode, l’objet DataTable auquel appartient l’objet DataRowCollection doit avoir au moins une colonne désignée comme colonne clé primaire. Lorsque deux lignes ou plus ont la même valeur de clé primaire, la première ligne trouvée est retournée. Cela se produit lorsqu’il EnforceConstraints est défini sur false. Consultez la propriété pour plus d’informations sur la PrimaryKey création d’une colonne ou d’un PrimaryKey tableau d’objets DataColumn lorsque la table possède plusieurs clés primaires.
Voir aussi
S’applique à
Find(Object)
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
- Source:
- DataRowCollection.cs
Obtient la ligne spécifiée par la valeur de clé primaire.
public:
System::Data::DataRow ^ Find(System::Object ^ key);
public System.Data.DataRow? Find(object? key);
public System.Data.DataRow Find(object key);
member this.Find : obj -> System.Data.DataRow
Public Function Find (key As Object) As DataRow
Paramètres
Retours
Qui DataRow contient la valeur de clé primaire spécifiée ; sinon, une valeur Null si la valeur de clé primaire n’existe pas dans le DataRowCollection.
Exceptions
La table n’a pas de clé primaire.
Exemples
L’exemple suivant utilise la Find méthode pour rechercher la valeur de clé primaire « 2 » dans une collection d’objets DataRow . La méthode retourne l’objet spécifique DataRow vous permettant de modifier ses valeurs, selon les besoins.
private void FindInPrimaryKeyColumn(DataTable table,
long pkValue)
{
// Find the number pkValue in the primary key
// column of the table.
DataRow foundRow = table.Rows.Find(pkValue);
// Print the value of column 1 of the found row.
if(foundRow != null)
Console.WriteLine(foundRow[1]);
}
Private Sub FindInPrimaryKeyColumn(ByVal table As DataTable, _
ByVal pkValue As Long)
' Find the number pkValue in the primary key
' column of the table.
Dim foundRow As DataRow = table.Rows.Find(pkValue)
' Print the value of column 1 of the found row.
If Not (foundRow Is Nothing) Then
Console.WriteLine(foundRow(1).ToString())
End If
End Sub
Remarques
Pour utiliser la Find méthode, l’objet DataTable auquel appartient l’objet DataRowCollection doit avoir au moins une colonne désignée comme colonne clé primaire. Pour plus d’informations sur la création d’une colonne clé primaire, consultez la PrimaryKey propriété.