SqlDataReader.GetValues(Object[]) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Preenche um array de objetos com os valores das colunas da linha atual.
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
Parâmetros
Devoluções
O número de instâncias de Object no array.
Implementações
Exemplos
O exemplo seguinte demonstra o uso de um array de tamanho correto para ler todos os valores da linha atual no .SqlDataReader Além disso, a amostra demonstra o uso de um array de tamanho fixo que pode ser menor ou maior do que o número de colunas disponíveis.
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
Observações
Para a maioria das aplicações, este método fornece um meio eficiente de recuperar todas as colunas, em vez de recuperar cada coluna individualmente.
Pode passar um Object array que contenha menos do que o número de colunas contidas na linha resultante. Apenas a quantidade de dados que o Object array contém é copiada para o array. Também pode passar um Object array cujo comprimento é superior ao número de colunas contidas na linha resultante.
Este método retorna DBNull para colunas de base de dados nulas.