SqlDataReader.GetValues(Object[]) 方法

定義

使用目前資料列的資料行值填入物件陣列。Populates an array of objects with the column values of the current row.

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

參數

values
Object[]

要複製屬性資料行的目的 Object 陣列。An array of Object into which to copy the attribute columns.

傳回

陣列中 Object 的執行個體數目。The number of instances of Object in the array.

實作

範例

下列範例示範如何使用正確大小的陣列,從提供的 SqlDataReader中讀取目前資料列的所有值。The following example demonstrates using a correctly sized array to read all values from the current row in the supplied SqlDataReader. 此外,此範例也會示範如何使用固定大小的陣列,其可能小於或等於可用的資料行數目。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

備註

對於大部分的應用程式而言,這個方法會提供有效率的方式來抓取所有的資料行,而不是個別抓取每個資料行。For most applications, this method provides an efficient means for retrieving all columns, instead of retrieving each column individually.

您可以傳遞 Object 陣列,其中包含所產生資料列中所包含的資料行數目。You can pass an Object array that contains fewer than the number of columns contained in the resulting row. 只有 Object 陣列所保存的資料量會複製到陣列中。Only the amount of data the Object array holds is copied to the array. 您也可以傳遞 Object 陣列,其長度大於產生的資料列中所包含的資料行數目。You can also pass an Object array whose length is more than the number of columns contained in the resulting row.

對於 null 資料庫資料行來說,這個方法會傳回 DBNullThis method returns DBNull for null database columns.

適用於

另請參閱