SqlDataReader.GetValues(Object[]) Método
Definição
Popula uma matriz de objetos com os valores da coluna da linha atual.Populates an array of objects with the column values of the current row.
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
Parâmetros
- values
- Object[]
Uma matriz de Object na qual as colunas de atributo serão copiadas.An array of Object into which to copy the attribute columns.
Retornos
O número de instâncias de Object na matriz.The number of instances of Object in the array.
Implementações
Exemplos
O exemplo a seguir demonstra o uso de uma matriz dimensionada corretamente para ler todos os valores da linha atual no fornecido SqlDataReader .The following example demonstrates using a correctly sized array to read all values from the current row in the supplied SqlDataReader. 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.In addition, the sample demonstrates using a fixed-sized array that could be either smaller or larger than the number of available columns.
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
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.For most applications, this method provides an efficient means for retrieving all columns, instead of retrieving each column individually.
Você pode passar uma Object matriz que contém menos do que o número de colunas contidas na linha resultante.You can pass an Object array that contains fewer than the number of columns contained in the resulting row. Somente a quantidade de dados que a Object matriz contém é copiada para a matriz.Only the amount of data the Object array holds is copied to the array. Você também pode passar uma Object matriz cujo comprimento é maior que o número de colunas contidas na linha resultante.You can also pass an Object array whose length is more than the number of columns contained in the resulting row.
Esse método retorna DBNull para colunas de banco de dados nulas.This method returns DBNull for null database columns.