2011-09-19 3 views
0

Visual Studio 2010 구성 마법사를 사용하여 만든 강력한 형식의 데이터 집합이 있습니다. 기본 키를 알고있는 한 DataRow를 쉽게 찾을 수 있습니다 (How to: Edit Rows in a DataTable 참조).Custom DataSet.Table.FindBy 메서드

PK를 모르는 경우 문제가 발생합니다. 복합 기본 키 (고유 제한 조건)가 될 수있는 열의 조합이있는 경우 DataRow를 반환하는 사용자 지정 메서드를 만드는 방법이 있습니까? 링크에서 예제를 사용하여, 나는 같은 것을 할 싶습니다 :

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

이 자신의 Northwind를 DB 고객의 표는 복합 키가 될 수 두 개의 열 (이름 및 유형)가 가정합니다. 그리고하여 FindBYCustomerNameAndType 방법은

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

답변

1
string whereClause = "name = 'TestCustomerName' and type = 'TestCustomerType'"; 
if (northwindDataSet1.Customers.Select(whereClause).GetLength(0) > 0) 
    CustomersRow customersRow = (northwindDataSet1.Customers.Select(whereClause)[0] as CustomersRow); 
에 매핑하는 것