DataSet ds = new DataSet();
DataTable table1 = new DataTable("tab1");
DataTable table2 = new DataTable("tab2");
DataTable table3 = new DataTable("tab3");
ds.Tables.Add(table1);
ds.Tables.Add(table2);
ds.Tables.Add(table3);
DataColumn dc1 = new DataColumn("a");
DataColumn dcc1 = new DataColumn("b");
DataColumn dc2 = new DataColumn("c");
DataColumn dcc2 = new DataColumn("d");
ds.Tables["tab1"].Columns.Add(dc1);
ds.Tables["tab1"].Columns.Add(dcc1);
ds.Tables["tab2"].Columns.Add(dc2);
ds.Tables["tab2"].Columns.Add(dcc2);
DataRow dr = ds.Tables["tab1"].NewRow();
dr["a"] = "1";
dr["b"] = "贺春林";
ds.Tables["tab1"].Rows.Add(dr);
DataRow drc = ds.Tables["tab2"].NewRow();
drc["c"] = "3";
drc["d"] = "我的地盘";
ds.Tables["tab2"].Rows.Add(drc);
DataRow dccc = ds.Tables["tab2"].NewRow();
dccc["c"] = "1";
dccc["d"] = "在在";
ds.Tables["tab2"].Rows.Add(dccc);
// Get the DataColumn objects from two DataTable objects in a DataSet.
DataColumn parentCol;
DataColumn childCol;
// Code to get the DataSet not shown here.
parentCol = ds.Tables["tab1"].Columns["a"];
childCol = ds.Tables["tab2"].Columns["c"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol , true);
// Add the relation to the DataSet.
ds.Relations.Add(relCustOrder);以上是我写的测试代码,为什么会出错。错误信息为:
This constraint cannot be enabled as not all values have corresponding parent values.
难道做两个DataSet里面的两个表关联时,一定要键值严格保持相等吗?有什么属性设置吗???
等待中....................在线....................................
DataTable table1 = new DataTable("tab1");
DataTable table2 = new DataTable("tab2");
DataTable table3 = new DataTable("tab3");
ds.Tables.Add(table1);
ds.Tables.Add(table2);
ds.Tables.Add(table3);
DataColumn dc1 = new DataColumn("a");
DataColumn dcc1 = new DataColumn("b");
DataColumn dc2 = new DataColumn("c");
DataColumn dcc2 = new DataColumn("d");
ds.Tables["tab1"].Columns.Add(dc1);
ds.Tables["tab1"].Columns.Add(dcc1);
ds.Tables["tab2"].Columns.Add(dc2);
ds.Tables["tab2"].Columns.Add(dcc2);
DataRow dr = ds.Tables["tab1"].NewRow();
dr["a"] = "1";
dr["b"] = "贺春林";
ds.Tables["tab1"].Rows.Add(dr);
DataRow drc = ds.Tables["tab2"].NewRow();
drc["c"] = "3";
drc["d"] = "我的地盘";
ds.Tables["tab2"].Rows.Add(drc);
DataRow dccc = ds.Tables["tab2"].NewRow();
dccc["c"] = "1";
dccc["d"] = "在在";
ds.Tables["tab2"].Rows.Add(dccc);
// Get the DataColumn objects from two DataTable objects in a DataSet.
DataColumn parentCol;
DataColumn childCol;
// Code to get the DataSet not shown here.
parentCol = ds.Tables["tab1"].Columns["a"];
childCol = ds.Tables["tab2"].Columns["c"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol , true);
// Add the relation to the DataSet.
ds.Relations.Add(relCustOrder);以上是我写的测试代码,为什么会出错。错误信息为:
This constraint cannot be enabled as not all values have corresponding parent values.
难道做两个DataSet里面的两个表关联时,一定要键值严格保持相等吗?有什么属性设置吗???
等待中....................在线....................................
解决方案 »
- 在C#中怎样从linkLable标签连接到网页(不用Process.Start())????
- 请大家提提意见,这个客服系统怎么做?
- c#有米有像c++ primer 或 think in java 这样的经典书籍
- 用C#怎么实现图片中的那个“编码”框??
- C#中,如何通过两个特定的字符将一个字符串转化为一个DataSet
- datagrid中如何在一行中显示多条记录!在线等,分全给了啊
- 也是将sql导出到access中的问题
- 做图形控件软件开发的朋友可以收藏 试用一下。
- 哪里有下载 最新 visual stuido .net 企业版的?速度快的
- 升4角了,散分。顺便把以前翻译的两篇关于组件编程的文章给大家参考
- C#中有没有类似VSGetLastErro的函数来捕捉意外错误。
- C#中如何在Form2中访问form1中的control TextBox
DataColumn dc2 = new DataColumn("c",GetType("System.String"));
This constraint cannot be enabled as not all values have corresponding parent values. 急呀,,,,,,,,,,,等待中........................................
DataRow drc = ds.Tables["tab2"].NewRow();
drc["c"] = "3";
drc["d"] = "我的地盘";
ds.Tables["tab2"].Rows.Add(drc);变为
DataRow drc = ds.Tables["tab2"].NewRow();
drc["c"] = "1";
drc["d"] = "我的地盘";
ds.Tables["tab2"].Rows.Add(drc);或者 再给table1加一行
DataRow dr = ds.Tables["tab1"].NewRow();
dr["a"] = "3";
dr["b"] = "测试";
ds.Tables["tab1"].Rows.Add(dr);
"his constraint cannot be enabled as not all values have corresponding parent values."不是所有的关联值都存在父值你tab1 中的行数少于 tab2
tab1中只有ID:1
tab2中却有ID:1,3你要么在tab2中加一行ID为3的数据,要么把 parentCol, childCol 两个掉过来!
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol , true);
变为
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol , false);