0

尝试直接在子表中获取父 ID,是否可以dsTest.Relations.Add()通过添加 cascade.for 级联来获取子表中的父 ID如何使用自动生成的标识密钥更新数据集父子表?

数据集 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();
4

0 回答 0