共用方式為


SqlDataReader.GetValues(Object[]) 方法

定義

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

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

參數

values
Object[]

要複製屬性資料行的目的 Object 陣列。

傳回

陣列中 Object 的執行個體數目。

實作

範例

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

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

備註

對於大部分的應用程式,這個方法提供有效率的方式擷取所有數據行,而不是個別擷取每個數據行。

您可以傳遞 Object 包含少於所產生資料列內含資料行數目的陣列。 只有陣列保留的數據 Object 量會複製到陣列。 您也可以傳遞 Object 長度超過結果數據列中所含數據行數目的陣列。

對於 null 資料庫資料行來說,這個方法會傳回 DBNull

適用於

另請參閱