Share via


DataTableReader.GetValues(Object[]) 方法

定義

使用目前資料列的資料行值填入物件陣列。

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

參數

values
Object[]

Object 的陣列,要從 DataTableReader 複製資料行值至其中。

傳回

複製至陣列的資料行值數目。

例外狀況

傳遞的索引超出 0 到 FieldCount - 1 的範圍。

嘗試從已刪除的資料列擷取資料。

嘗試在關閉的 DataTableReader 中讀取或存取資料行。

範例

下列範例示範如何使用正確大小的陣列,從所提供的 DataTableReader中目前數據列讀取所有值。 此外,此範例示範如何使用可能小於或大於可用數據行數目的固定大小陣列。

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

備註

對於大部分的應用程式,這個方法提供有效率的方式擷取所有數據行,而不是個別擷取每個數據行。 如果您的意圖是從 中的數據 DataTableReader列擷取所有數據行值,此方法 GetValues 會提供最有效率的解決方案。

您可以傳遞 Object 陣列,其中包含少於結果數據列中包含的數據行數目。 只有數位可以儲存的數據 Object 量會複製到陣列。 您也可以傳遞 Object 長度超過結果數據列中所含數據行數目的陣列,在此情況下,方法呼叫會保持不變其他陣列元素。

這個方法會將 null 資料行的輸出數位中放置 DBNull

適用於