大多数使用数据的应用程序需要访问满足某种条件的特定记录。为了找到数据集中的特定行,可调用 DataRow 对象。如果找不到主键,则返回空值。

在类型化的数据集中查找具有主键值的行

  • 调用使用表的主键来定位行的强类型 FindBy 方法。

    在下面的示例中,CustomerID 列是 Customers 表的主键,因此生成的 FindBy 方法为 FindByCustomerID。此示例演示如何使用生成的 FindBy 方法将特定的 DataRow 赋给变量。

    Visual Basic
    As NorthwindDataSet.CustomersRow
        customersRow = NorthwindDataSet1.Customers.FindByCustomerID("ALFKI")
        
    NorthwindDataSet.CustomersRow customersRow =
        northwindDataSet1.Customers.FindByCustomerID("ALFKI");
        
    NorthwindDataSet.CustomersRow customersRow =
        northwindDataSet1.get_Customers().FindByCustomerID("ALFKI");
        

在非类型化的数据集中查找具有主键值的行

  • 调用 DataRowCollection 集合的 Find 方法,同时将主键作为参数传递给它。

    下面的示例演示如何声明新行(名为 foundRow)并将 Find 方法的返回值分配给它。如果找到主键,消息框中将显示列索引 1 的内容。

    Visual Basic
        Dim foundRow As DataRow = DataSet1.Tables("AnyTable").Rows.Find(s)
        If foundRow IsNot Nothing Then
        MsgBox(foundRow(1).ToString())
        Else
        MsgBox("A row with the primary key of " & s & " could not be found")
        End If
        
    ;
        DataRow foundRow = dataSet1.Tables["AnyTable"].Rows.Find(s);
        if (foundRow != null)
        {
        MessageBox.Show(foundRow[1].ToString());
        }
        else
        {
        MessageBox.Show("A row with the primary key of " + s + " could not be found");
        }
        
    ;
        DataRow foundRow = dataSet1.get_Tables().get_Item("AnyTable").get_Rows().Find(s);
        if (foundRow != null)
        {
        MessageBox.Show(foundRow.get_Item(1).toString());
        }
        else
        {
        MessageBox.Show("A row with he primary key of " + s + " could not be found.");
        }
        

通过列值查找行

基于任意列中的值查找行

  • 使用 Expression 属性的页的“表达式语法”部分。

    下面的示例演示如何使用 DataTableSelect 方法来定位特定行。

    Visual Basic
    As Data.DataRow
        foundRows = DataSet1.Tables("Customers").Select("CompanyName Like 'A%'")
        
    DataRow[] foundRows;
        foundRows = dataSet1.Tables["Customers"].Select("CompanyName Like 'A%'");
        
    DataRow[] foundRows;
        foundRows = dataSet1.get_Tables().get_Item("Customers").Select("CompanyName Like 'A%'");
        

相关文章:

  • 2022-01-15
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2021-10-29
  • 2021-11-19
  • 2021-07-24
  • 2021-04-01
猜你喜欢
  • 2021-09-24
  • 2021-12-26
  • 2022-03-01
  • 2021-12-26
  • 2021-05-04
  • 2021-06-18
相关资源
相似解决方案