Export (0) Print
Expand All

DataTableReader.GetValues Method

Populates an array of objects with the column values of the current row.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public override int GetValues(
	Object[] values
)

Parameters

values
Type: System.Object[]

An array of Object into which to copy the column values from the DataTableReader.

Return Value

Type: System.Int32
The number of column values copied into the array.

Implements

IDataRecord.GetValues(Object[])

ExceptionCondition
ArgumentOutOfRangeException

The index passed was outside the range of 0 to FieldCount - 1.

DeletedRowInaccessibleException

An attempt was made to retrieve data from a deleted row.

InvalidOperationException

An attempt was made to read or access a column in a closed DataTableReader .

For most applications, this method provides an efficient means for retrieving all columns, instead of retrieving each column individually. If your intent is to retrieve all the column values from a row within the DataTableReader, the GetValues method provides the most efficient solution.

You can pass an Object array that contains fewer than the number of columns that are contained in the resulting row. Only the amount of data the Object array can hold is copied to the array. 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.

This method places DBNull in the output array for null columns.

The 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]);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft