如何在c#中将父表从父表反映到子表

时间:2016-12-20 08:35:17

标签: c# datarelation

尝试直接在子表中获取父ID,是否可以通过使用dsTest.Relations.Add()来获取子表中的父ID,同时尝试添加cascade.for cascade How to update Dataset Parent & Child tables with Autogenerated Identity Key?

DataSet dsTest = new DataSet(“DataSet”);

       DataTable parentTable = new DataTable("Parents");

        parentTable.Columns.Add("ParentId", typeof(int));

        parentTable.Columns.Add("ParentName", typeof(string));

        //Create some parents.

        parentTable.Rows.Add(new object[] { 1, "Parent # 1" });

        parentTable.Rows.Add(new object[] { 2, "Parent # 2" });

        parentTable.Rows.Add(new object[] { 3, "Parent # 3" });

        dsTest.Tables.Add(parentTable);


        // Table for childrend

        DataTable childTable = new DataTable("Childs");

        childTable.Columns.Add("ChildId", typeof(int));

        childTable.Columns.Add("ChildName", typeof(string));

        childTable.Columns.Add("ParentId", typeof(int));

        //Create some childs.

        childTable.Rows.Add(new object[] { 1, "Child # 1", });

        childTable.Rows.Add(new object[] { 2, "Child # 2", });

        childTable.Rows.Add(new object[] { 3, "Child # 3", });

        childTable.Rows.Add(new object[] { 4, "Child # 4", });

        childTable.Rows.Add(new object[] { 5, "Child # 5", });

        dsTest.Tables.Add(childTable);

        //// Create their relation.

        DataRelation parentChildRelation = new DataRelation("ParentChild", parentTable.Columns["ParentId"], childTable.Columns["ParentId"]);

        dsTest.Relations.Add(parentChildRelation);

        childTable.AcceptChanges();

0 个答案:

没有答案