DataTableReader.GetValues(Object[]) 方法

定义

使用当前行的列值来填充对象数组。Populates an array of objects with the column values of the current row.

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 数组。An array of Object into which to copy the column values from the DataTableReader.

返回

Int32

复制到数组中的列值个数。The number of column values copied into the array.

例外

传递的索引超出了 0 到 FieldCount -1 的范围。The index passed was outside the range of 0 to FieldCount - 1.

尝试从已删除的行中检索数据。An attempt was made to retrieve data from a deleted row.

尝试读取或访问已关闭 DataTableReader 中的列。An attempt was made to read or access a column in a closed DataTableReader .

示例

下面的示例演示如何使用大小正确的数组从提供的中读取当前行中的所有值 DataTableReaderThe following example demonstrates using an array that is the correct size, to read all values from the current row in the supplied DataTableReader. 此外,该示例还演示了如何使用大小固定的数组,该数组可以小于或大于可用列数。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(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

注解

对于大多数应用程序,此方法提供了一种有效的方法来检索所有列,而不是单独检索每个列。For most applications, this method provides an efficient means for retrieving all columns, instead of retrieving each column individually. 如果您的目的是从内的行中检索所有列值 DataTableReader ,则方法将 GetValues 提供最有效的解决方案。If your intent is to retrieve all the column values from a row within the DataTableReader, the GetValues method provides the most efficient solution.

可以传递一个 Object 数组,该数组包含的列数少于所产生行中包含的列数。You can pass an Object array that contains fewer than the number of columns that are contained in the resulting row. 只有 Object 数组可以保存的数据量会复制到数组中。Only the amount of data the Object array can hold is copied to the array. 还可以传递 Object 其长度大于结果行中包含的列数的数组,在这种情况下,其他数组元素将在方法调用中保持不变。You can also pass an Object array whose length is more than the number of columns that are contained in the resulting row, in which case the additional array elements remains unchanged by the method call.

此方法 DBNull 在输出数组中放置 null 列。This method places DBNull in the output array for null columns.

适用于