DataTableReader.GetValues(Object[]) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用目前數據列的數據行值填入 物件的陣列。
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 長度超過該列中欄位數的陣列,此時額外的陣列元素不會因方法呼叫而改變。
此方法將空欄位放入 DBNull 輸出陣列。