使用GetChildRows为什么不能获得行状态标记为DataRowState.Added的所有子行??急……
sy246 2005-01-23 12:26:22 建立关系
ds.Relations.Clear();
DataColumn parentCol;
DataColumn childCol;
parentCol = ds.Tables["DictionaryMain"].Columns["ID"];
childCol = ds.Tables["DictionaryDetail"].Columns["ID"];
DataRelation relCustOrder;
string relationname = "ParentAndChild";
relCustOrder = new DataRelation(relationname, parentCol, childCol);
ds.Relations.Add(relCustOrder);
添加新行
DataRow row = ds.Tables["DictionaryMain"].NewRow();//父行
row["ID"] = "111";
row["TableName"] = "zlzllz";
ds.Tables["DictionaryMain"].Rows.Add(row);
DataRow rows = ds.Tables["DictionaryDetail"].NewRow();//子行
rows["ID"] = "111";
rows["FileID"]="1";
rows["FileName"] = "name";
rows.SetParentRow(row);
ds.Tables["DictionaryDetail"].Rows.Add(rows);
//获取新添加的行以及其子行来保存
foreach(DataRow row in ds.Tables["DictionaryMain"].GetChanges(DataRowState.Added).Rows)
{
string sql = "insert into DictionaryMain(TableName) values('"+row["TableName"].ToString()+"');";
sql += "SELECT @ID=@@IDENTITY;";
DataRow[] childRow = row.GetChildRows("ParentAndChild");//在次并没有找到他的子行
foreach(DataRow rows in childRow)
{
sql += "insert into DictionaryDetail(ID,FileID,FileName) "+"values(@ID,"+detailID.ToString()+",'"+rows["FileName"].ToString()+"');";
detailID++;
}
err = WebInvoke.DataInvoke.Execute(sql);
if(err!="")
{
MessageBox.Show("保存表字段信息时出错!原因为:"+err);
return;
}
}
//但是问题是我可以通过DataRow[] childRow = row.GetChildRows("ParentAndChild");来获得刚从数据库中读取出来的数据的所有子行!
请高手赐教!谢谢