DataTableReader.GetValues(Object[]) Metoda

Definicja

Wypełnia tablicę obiektów wartościami kolumn bieżącego wiersza.

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

Parametry

values
Object[]

Tablica, do której Object mają być skopiowane wartości kolumn z obiektu DataTableReader.

Zwraca

Liczba wartości kolumn skopiowanych do tablicy.

Wyjątki

Przekazany indeks był poza zakresem od 0 do FieldCount -1.

Podjęto próbę pobrania danych z usuniętego wiersza.

Podjęto próbę odczytu lub uzyskania dostępu do kolumny w zamkniętej DataTableReader kolumnie .

Przykłady

W poniższym przykładzie pokazano użycie tablicy o poprawnym rozmiarze, aby odczytać wszystkie wartości z bieżącego wiersza w podanym DataTableReaderelemencie . Ponadto w przykładzie pokazano użycie tablicy o stałym rozmiarze, która może być mniejsza lub większa niż liczba dostępnych kolumn.

private static void TestGetValues(DataTableReader reader)
{
    // Given a DataTableReader, 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 DataTableReader)

    ' Given a DataTableReader, 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

Uwagi

W przypadku większości aplikacji ta metoda zapewnia wydajny sposób pobierania wszystkich kolumn zamiast pobierania poszczególnych kolumn osobno. Jeśli Twoim celem jest pobranie wszystkich wartości kolumn z wiersza w obiekcie DataTableReader, GetValues metoda zapewnia najbardziej wydajne rozwiązanie.

Można przekazać tablicę Object zawierającą mniej niż liczbę kolumn zawartych w wierszu wynikowym. Tylko ilość danych, które Object może przechowywać tablica, jest kopiowana do tablicy. Można również przekazać tablicę Object , której długość jest większa niż liczba kolumn zawartych w wierszu wynikowym, w tym przypadku dodatkowe elementy tablicy pozostają niezmienione przez wywołanie metody.

Ta metoda umieszcza DBNull w tablicy wyjściowej kolumny o wartości null.

Dotyczy