Share via


DataTableReader.GetValues(Object[]) Método

Definição

Popula uma matriz de objetos com os valores da coluna da linha atual.

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

Parâmetros

values
Object[]

Uma matriz de Object para a qual copiar os valores da coluna de DataTableReader.

Retornos

O número de valores de coluna copiados para a matriz.

Exceções

O índice passado estava fora do intervalo de 0 a FieldCount -1.

Foi feita uma tentativa de recuperar dados de uma linha excluída.

Foi feita uma tentativa de ler ou acessar uma coluna em um DataTableReader fechado.

Exemplos

O exemplo a seguir demonstra o uso de uma matriz que é o tamanho correto para ler todos os valores da linha atual no fornecido DataTableReader. Além disso, o exemplo demonstra o uso de uma matriz de tamanho fixo que pode ser menor ou maior do que o número de colunas disponíveis.

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

Comentários

Para a maioria dos aplicativos, esse método fornece um meio eficiente para recuperar todas as colunas, em vez de recuperar cada coluna individualmente. Se sua intenção for recuperar todos os valores de coluna de uma linha dentro do DataTableReader, o GetValues método fornecerá a solução mais eficiente.

Você pode passar uma Object matriz que contém menos do que o número de colunas contidas na linha resultante. Somente a quantidade de dados que a Object matriz pode conter é copiada para a matriz. Você também pode passar uma Object matriz cujo comprimento é maior que o número de colunas contidas na linha resultante, nesse caso, os elementos de matriz adicionais permanecem inalterados pela chamada de método.

Esse método coloca DBNull na matriz de saída para colunas nulas.

Aplica-se a