【问题标题】:Custom DataSet.Table.FindBy method自定义 DataSet.Table.FindBy 方法
【发布时间】:2011-11-20 15:23:41
【问题描述】:

我有一个使用 Visual Studio 2010 的配置向导创建的强类型数据集。只要我知道主键,我就可以轻松找到 DataRow(请参阅How to: Edit Rows in a DataTable)。

当我不知道PK时,就会出现问题。如果您的列组合也可能是复合主键(唯一约束),是否有办法创建返回 DataRow 的自定义方法。使用链接中的示例,我想做这样的事情:

NorthwindDataSet.CustomersRow customersRow = northwindDataSet1.Customers.FindByCustomerNameAndType("TestCustomerName", "TestCustomerType");

这假定他们的 Northwind DB 客户表有两列(名称和类型),它们也可以是一个复合键。 FindBYCustomerNameAndType 方法将映射到

 SELECT *
 FROM Customers
 WHERE name = "TestCustomerName" AND type = "TestCustomerType"

【问题讨论】:

    标签: c# .net sql dataset strongly-typed-dataset


    【解决方案1】:
    string whereClause = "name = 'TestCustomerName' and type = 'TestCustomerType'";
    DataRow[] x = northwindDataSet1.Customers.Select(whereClause);
    if (x.Length > 0){
        CustomersRow customersRow = x[0] as CustomersRow;
    
        //other code here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      相关资源
      最近更新 更多