DataRowCollection.Find 方法

定義

使用指定的 PrimaryKey 值取得 DataRowGets a DataRow using the specified PrimaryKey value.

多載

Find(Object[])

取得包含指定主索引鍵值的資料列。Gets the row that contains the specified primary key values.

Find(Object)

取得主索引鍵值指定的資料列。Gets the row specified by the primary key value.

備註

效能應為 (log n) 作業的 O。Performance should be an O(log n) operation.

Find(Object[])

取得包含指定主索引鍵值的資料列。Gets the row that contains the specified primary key values.

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[]

要尋找的主索引鍵值的陣列。An array of primary key values to find. 陣列的型別為 ObjectThe type of the array is Object.

傳回

DataRow

DataRow 物件,其包含指定的主索引鍵值;如果 DataRowCollection 中沒有主索引鍵值,則傳回 null 值。A DataRow object that contains the primary key values specified; otherwise a null value if the primary key value does not exist in the DataRowCollection.

例外狀況

沒有資料列對應至該索引值。No row corresponds to that index value.

資料表沒有主索引鍵。The table does not have a primary key.

範例

下列範例會使用陣列的值來尋找物件集合中的特定資料列 DataRowThe following example uses the values of an array to find a specific row in a collection of DataRow objects. 方法會假設 DataTable 存在有三個主鍵資料行的。The method assumes that a DataTable exists with three primary key columns. 在建立值的陣列之後,程式碼會使用 Find 方法與陣列來取得您想要的特定物件。After creating an array of the values, the code uses the Find method with the array to get the particular object that you want.

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 物件所屬的物件必須至少有一個資料行指定為主鍵資料行。To use the Find method, the DataTable object to which the DataRowCollection object belongs must have at least one column designated as a primary key column. 如果有兩個或多個資料列具有相同的主鍵值,則會傳回第一個找到的資料列。When two or more rows have the same primary key value, then the first row found is returned. 當設定為 false 時,就會發生這種情況 EnforceConstraintsThis occurs when EnforceConstraints is set to false. 如需 PrimaryKey 有關如何建立資料行的詳細資訊 PrimaryKey ,或 DataColumn 當資料表有一個以上的主鍵時,如何建立物件陣列的詳細資訊,請參閱屬性。See the PrimaryKey property for more information about how to create a PrimaryKey column, or an array of DataColumn objects when the table has more than one primary key.

另請參閱

適用於

Find(Object)

取得主索引鍵值指定的資料列。Gets the row specified by the primary key value.

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 的主索引鍵值。The primary key value of the DataRow to find.

傳回

DataRow

DataRow,其中包含指定的主索引鍵值,否則,如果 DataRowCollection 中沒有主索引鍵值,就傳回 Null 值。A DataRow that contains the primary key value specified; otherwise a null value if the primary key value does not exist in the DataRowCollection.

例外狀況

資料表沒有主索引鍵。The table does not have a primary key.

範例

下列範例會使用 Find 方法,在物件的集合中尋找主要索引鍵值 "2" DataRowThe following example uses the Find method to find the primary key value "2" in a collection of DataRow objects. 方法會傳回特定 DataRow 物件,讓您視需要變更其值。The method returns the specific DataRow object letting you change its values, as needed.

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 物件所屬的物件必須至少有一個資料行指定為主鍵資料行。To use the Find method, the DataTable object to which the DataRowCollection object belongs must have at least one column designated as a primary key column. 如需 PrimaryKey 如何建立主鍵資料行的詳細資訊,請參閱屬性。See the PrimaryKey property for more information about how to create a primary key column.

另請參閱

適用於