DataRowCollection.Find 方法

定义

使用指定的 DataRow 值获取 PrimaryKeyGets 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.

注解

性能应为 O (log n)操作。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);
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 对象;如果 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.

示例

下面的示例使用数组的值查找 DataRow 对象集合中的特定行。The following example uses the values of an array to find a specific row in a collection of DataRow objects. 方法假设存在具有三个主键列的 DataTableThe 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 方法,则 DataRowCollection 对象所属的 DataTable 对象必须至少有一个指定为主键列的列。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. 如果 EnforceConstraints 设置为 false,则会发生这种情况。This 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);
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;否则为空值(如果 DataRowCollection 中不存在主键值)。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 方法查找 DataRow 对象集合中的主键值 "2"。The 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 方法,则 DataRowCollection 对象所属的 DataTable 对象必须至少有一个指定为主键列的列。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.

另请参阅

适用于