SqlDataReader.GetValues(Object[]) Méthode

Définition

Remplit un tableau d’objets avec les valeurs de colonne de la ligne actuelle.

public:
 virtual int GetValues(cli::array <System::Object ^> ^ values);
public:
 override int GetValues(cli::array <System::Object ^> ^ values);
public int GetValues(object[] values);
public override int GetValues(object[] values);
abstract member GetValues : obj[] -> int
override this.GetValues : obj[] -> int
override this.GetValues : obj[] -> int
Public Function GetValues (values As Object()) As Integer
Public Overrides Function GetValues (values As Object()) As Integer

Paramètres

values
Object[]

Tableau dans Object lequel copier les colonnes d’attribut.

Retours

Nombre d’instances du Object tableau.

Implémente

Exemples

L’exemple suivant illustre l’utilisation d’un tableau correctement dimensionné pour lire toutes les valeurs de la ligne actuelle dans le tableau fourni SqlDataReader. En outre, l’exemple illustre l’utilisation d’un tableau de taille fixe qui peut être plus petit ou supérieur au nombre de colonnes disponibles.

private static void TestGetValues(SqlDataReader reader)
{
    // Given a SqlDataReader, use the GetValues
    // method to retrieve a full row of data.
    // Test the GetValues method, passing in an array large
    // enough for all the columns.
    Object[] values = new Object[reader.FieldCount];
    int fieldCount = reader.GetValues(values);

    Console.WriteLine("reader.GetValues retrieved {0} columns.",
        fieldCount);
    for (int i = 0; i < fieldCount; i++)
        Console.WriteLine(values[i]);

    Console.WriteLine();

    // Now repeat, using an array that may contain a different
    // number of columns than the original data. This should work correctly,
    // whether the size of the array is larger or smaller than
    // the number of columns.

    // Attempt to retrieve three columns of data.
    values = new Object[3];
    fieldCount = reader.GetValues(values);
    Console.WriteLine("reader.GetValues retrieved {0} columns.",
        fieldCount);
    for (int i = 0; i < fieldCount; i++)
        Console.WriteLine(values[i]);
}
Private Sub TestGetValues(ByVal reader As SqlDataReader)

    ' Given a SqlDataReader, use the GetValues
    ' method to retrieve a full row of data.

    ' Test the GetValues method, passing in an array large
    ' enough for all the columns.
    Dim values(reader.FieldCount - 1) As Object
    Dim fieldCount As Integer = reader.GetValues(values)
    Console.WriteLine("reader.GetValues retrieved {0} columns.", _
         fieldCount)
    For i As Integer = 0 To fieldCount - 1
        Console.WriteLine(values(i))
    Next

    Console.WriteLine()

    ' Now repeat, using an array that may contain a different 
    ' number of columns than the original data. This should work correctly,
    ' whether the size of the array is larger or smaller than 
    ' the number of columns.

    ' Attempt to retrieve three columns of data.
    ReDim values(2)
    fieldCount = reader.GetValues(values)
    Console.WriteLine("reader.GetValues retrieved {0} columns.", _
    fieldCount)
    For i As Integer = 0 To fieldCount - 1
        Console.WriteLine(values(i))
    Next
End Sub

Remarques

Pour la plupart des applications, cette méthode fournit un moyen efficace de récupérer toutes les colonnes, au lieu de récupérer chaque colonne individuellement.

Vous pouvez passer un Object tableau qui contient moins de colonnes contenues dans la ligne résultante. Seule la quantité de données Object contenues dans le tableau est copiée dans le tableau. Vous pouvez également passer un Object tableau dont la longueur est supérieure au nombre de colonnes contenues dans la ligne résultante.

Cette méthode retourne DBNull pour les colonnes de base de données Null.

S’applique à

Voir aussi