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 します。

適用対象