Friday, 27 September 2013

Compare datatables with unlike primary keys

Compare datatables with unlike primary keys

I have datatables A and B. Table A has columns 1 and 2. Columns 1 and 2
are the primary key. Table B has columns 1, 2, 4. Columns 1 and 4 are the
primary key. I want to update table B so that for every value where B.1 ==
A.1 I want to make it so that B.2 = A.2. Because 2 is not part of the
primary key for table B there may be multiple records where B.1 and B.2
are the same and I want to update 2 for all those rows.
I am stuck at this kind of code:
foreach(DataRow dr in A.Rows){
DataRow Found = B.Rows.Find(dr[1]);
if(Found != null)
Found[2] = dr[2];
}
The major problem I am facing is that because table B has a compound
primary key that is shared by table A. The find is looking for two values
but only one can come from table A.

No comments:

Post a Comment