SqlDataReader.GetValues(Object[]) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Popola una matrice di oggetti con i valori di colonna della riga corrente.
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
Parametri
Valori restituiti
Numero di istanze di Object nella matrice.
Implementazioni
Esempio
Nell'esempio seguente viene illustrato l'uso di una matrice con dimensioni corrette per leggere tutti i valori della riga corrente nell'oggetto fornito SqlDataReader. Inoltre, l'esempio illustra l'uso di una matrice a dimensione fissa che potrebbe essere minore o maggiore 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 modo efficiente per recuperare tutte le colonne, invece di recuperare ogni colonna singolarmente.
È possibile passare una Object matrice contenente meno del numero di colonne contenute nella riga risultante. Solo la quantità di dati contenuti nella Object matrice viene copiata nella matrice. È anche possibile passare una Object matrice la cui lunghezza è maggiore del numero di colonne contenute nella riga risultante.
Questo metodo restituisce DBNull per le colonne di database Null.