SqlDataReader.GetValues(Object[]) Metodo

Definizione

Popola una matrice di oggetti con i valori della colonna della riga corrente.

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

Parametri

values
Object[]

Matrice di Object in cui copiare le colonne attributo.

Restituisce

Numero di istanze di Object nella matrice.

Implementazioni

Esempio

Nell'esempio seguente viene illustrato l'uso di una matrice di dimensioni corretta per leggere tutti i valori della riga corrente nell'oggetto specificato SqlDataReader. Inoltre, l'esempio illustra l'uso di una matrice di dimensioni fisse che potrebbero essere più piccole o maggiori del numero di colonne disponibili.

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

Commenti

Per la maggior parte delle applicazioni, questo metodo offre un mezzo efficiente per recuperare tutte le colonne, anziché recuperare singolarmente ogni colonna.

È possibile passare una Object matrice contenente meno del numero di colonne contenute nella riga risultante. Solo la quantità di dati che la Object matrice contiene viene copiata nella matrice. È anche possibile passare una Object matrice la cui lunghezza è maggiore del numero di colonne contenute nella riga risultante.

Il metodo restituisce DBNull per colonne di database con valori null.

Si applica a

Vedi anche