DataRowCollection.Find 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用指定的PrimaryKey值得到 。DataRow
多載
| 名稱 | Description |
|---|---|
| Find(Object[]) |
取得包含指定主鍵值的列。 |
| Find(Object) |
取得由主鍵值指定的列。 |
備註
效能應該是 O(log n) 的操作。
Find(Object[])
取得包含指定主鍵值的列。
public:
System::Data::DataRow ^ Find(cli::array <System::Object ^> ^ keys);
public System.Data.DataRow? Find(object?[] keys);
public System.Data.DataRow Find(object[] keys);
member this.Find : obj[] -> System.Data.DataRow
Public Function Find (keys As Object()) As DataRow
參數
- keys
- Object[]
一組主要鍵值的數值需要尋找。 陣列的類型為 Object。
傳回
包含 DataRow 指定主鍵值的物件;若主鍵值不存在 DataRowCollection,則為空值。
例外狀況
沒有任何一列對應該索引值。
該表格沒有主鍵。
範例
以下範例利用陣列的值來尋找物件集合 DataRow 中的特定列。 此方法假設 a DataTable 存在三個主要鍵欄位。 建立一個數值陣列後,程式碼會用 Find 這個方法取得你想要的特定物件。
private void FindInMultiPKey(DataTable table)
{
// Create an array for the key values to find.
object[]findTheseVals = new object[3];
// Set the values of the keys to find.
findTheseVals[0] = "John";
findTheseVals[1] = "Smith";
findTheseVals[2] = "5 Main St.";
DataRow foundRow = table.Rows.Find(findTheseVals);
// Display column 1 of the found row.
if(foundRow != null)
Console.WriteLine(foundRow[1]);
}
Private Sub FindInMultiPKey(ByVal table As DataTable)
' Create an array for the key values to find.
Dim findTheseVals(2) As Object
' Set the values of the keys to find.
findTheseVals(0) = "John"
findTheseVals(1) = "Smith"
findTheseVals(2) = "5 Main St."
Dim foundRow As DataRow = table.Rows.Find(findTheseVals)
' Display column 1 of the found row.
If Not (foundRow Is Nothing) Then
Console.WriteLine(foundRow(1).ToString())
End If
End Sub
備註
使用此 Find 方法時, DataTable 該物件所屬的物件 DataRowCollection 必須至少有一欄指定為主鍵欄位。 當兩列或以上的主鍵值相同時,會回傳第一列。 當 EnforceConstraints 被設定為 false 時,會發生這種情況。 關於如何在資料表有多個主鍵時建立PrimaryKey欄位或物件陣列DataColumn,請參見該PrimaryKey性質。
另請參閱
適用於
Find(Object)
取得由主鍵值指定的列。
public:
System::Data::DataRow ^ Find(System::Object ^ key);
public System.Data.DataRow? Find(object? key);
public System.Data.DataRow Find(object key);
member this.Find : obj -> System.Data.DataRow
Public Function Find (key As Object) As DataRow
參數
傳回
DataRow A 包含指定的主要鍵值;否則若主鍵值不存在於 DataRowCollection。
例外狀況
該表格沒有主鍵。
範例
以下範例使用此 Find 方法來尋找一組 DataRow 物件中的主要鍵值「2」。 這個方法會回傳特定 DataRow 物件,讓你可以根據需要調整它的值。
private void FindInPrimaryKeyColumn(DataTable table,
long pkValue)
{
// Find the number pkValue in the primary key
// column of the table.
DataRow foundRow = table.Rows.Find(pkValue);
// Print the value of column 1 of the found row.
if(foundRow != null)
Console.WriteLine(foundRow[1]);
}
Private Sub FindInPrimaryKeyColumn(ByVal table As DataTable, _
ByVal pkValue As Long)
' Find the number pkValue in the primary key
' column of the table.
Dim foundRow As DataRow = table.Rows.Find(pkValue)
' Print the value of column 1 of the found row.
If Not (foundRow Is Nothing) Then
Console.WriteLine(foundRow(1).ToString())
End If
End Sub
備註
使用此 Find 方法時, DataTable 該物件所屬的物件 DataRowCollection 必須至少有一欄指定為主鍵欄位。 請參閱該 PrimaryKey 屬性以了解如何建立主鍵欄位。