DataRowCollection.Find 方法

定義

使用指定的 PrimaryKey 值取得 DataRow

多載

Find(Object[])

取得包含指定主索引鍵值的資料列。

Find(Object)

取得主索引鍵值指定的資料列。

備註

效能應該是 O (記錄 n) 作業。

Find(Object[])

來源:
DataRowCollection.cs
來源:
DataRowCollection.cs
來源:
DataRowCollection.cs

取得包含指定主索引鍵值的資料列。

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 中沒有主索引鍵值,則傳回 null 值。

例外狀況

沒有資料列對應至該索引值。

資料表沒有主索引鍵。

範例

下列範例會使用陣列的值來尋找 物件集合 DataRow 中的特定數據列。 方法假設 有三個 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 必須至少有一個數據行指定為主鍵數據行。 當兩個或多個數據列具有相同的主鍵值時,會傳回找到的第一個數據列。 當 設定為 false 時 EnforceConstraints ,就會發生這種情況。 PrimaryKey如需如何在數據表有多個主鍵時建立PrimaryKey數據行或物件數位DataColumn的詳細資訊,請參閱 屬性。

另請參閱

適用於

Find(Object)

來源:
DataRowCollection.cs
來源:
DataRowCollection.cs
來源:
DataRowCollection.cs

取得主索引鍵值指定的資料列。

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

參數

key
Object

要尋找之 DataRow 的主索引鍵值。

傳回

DataRow,其中包含指定的主索引鍵值,否則,如果 DataRowCollection 中沒有主索引鍵值,就傳回 Null 值。

例外狀況

資料表沒有主索引鍵。

範例

下列範例會 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 請參閱 屬性。

另請參閱

適用於