Multi-key relations

from the Artful MySQL Tips List


If two tables, transaction and detail. are related via multiple keys (TransID, CustomerID, SalePersonID), how to set the relationship in DataSet ds between the tables?
  • Declare a DataColumn array for each table
  • For each array pass the list of columns to the array constructor
  • Pass a string which is the name of your relation and the two arrays to the DataRelation constructor
  • Add the relation to the DataSet.
DataColumn[] TransactionColumns;
DataColumn[] DetailColumns;
TransactionColumns = new DataColumn[] {
  ds.Tables[0].Columns["TransID"], 
  ds.Table[0].Columns["CustomerID"],
  ds.Tables[0].Columns["SalePersonID"]
};
DetailColumns = new DataColumn[] {
  ds.Tables[1].Columns["TransID"], 
  ds.Table[1].Columns["CustomerID"], 
  ds.Tables[1].Columns["SalePersonID"]
};
DataRelation TransactionDetail = 
  new DataRelation( "dsDataRelation", TransactionColumns, DetailColumns );
ds.Relations.Add(TransactionDetail);

Last updated 22 May 2024


Return to the Artful MySQL Tips page