1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
public static void GetChildRowsFromDataRelation()
{
/* For each row in the table, get the child rows using the
ChildRelations. For each item in the array, print the value
of each column. */
DataTable table = CreateDataSet().Tables["PART_ORACLE"];
DataRow[] childRows;
foreach (DataRelation PART_DB2_PART_ORACLE in table.ChildRelations)
{
foreach (DataRow row in table.Rows)
{
PrintRowValues(new DataRow[] { row }, "Parent Row");
childRows = row.GetChildRows(PART_DB2_PART_ORACLE);
// Print values of rows.
PrintRowValues(childRows, "child rows");
}
}
}
public DataRelationCollection ChildRelations { get; }
public static DataSet CreateDataSet()
{
// create a DataSet with one table, two columns
DataSet dataSet = new DataSet();
// create PART_DB2 table
DataTable table = new DataTable("PART_DB2");
dataSet.Tables.Add(table);
table.Columns.Add("CTIDNO",typeof(string));
table.Columns.Add("RCDLOC",typeof(string));
table.PrimaryKey = new DataColumn[] { table.Columns["CTIDNO"] };
// create PART_ORACLE
table = new DataTable("PART_ORACLE");
dataSet.Tables.Add(table);
table.Columns.Add("CTIDNO",typeof(string));
table.Columns.Add("RCPT_ZONE",typeof(string));
table.PrimaryKey = new DataColumn[] { table.Columns["CTIDNO"] };
// create relation
dataSet.Relations.Add(dataSet.Tables["PART_DB2"].Columns["CTIDNO"],
dataSet.Tables["PART_ORACLE"].Columns["CTIDNO"]);
// populate the tables
// add PART_DB2 record
dataSet.Tables["PART_DB2"].Rows.Add(
new object[] { CTIDNO });
dataSet.Tables["PART_ORACLE"].Rows.Add(
new object[] {CTIDNO,AVGLEADTIME});
return dataSet;
}
private static void PrintRowValues(DataRow[] rows, string label)
{
Console.WriteLine("\n{0}", label);
if (rows.Length <= 0)
{
Console.WriteLine("no rows found");
return;
}
foreach (DataRow row in rows)
{
foreach (DataColumn column in row.Table.Columns)
{
Console.Write("\table {0}", row[column]);
}
Console.WriteLine();
}
} |
Partager