[英]is it possible to add DataRelation to DataSet if child table contains rows that have no parent in parent table in C# / ADO.NET 2.0
If I fill the DataSet with DataAdapters that select all rows from Orders and Customers and call: 如果我使用DataAdapter填充DataSet,从Dataders和Customers中选择所有行并调用:
private void CreateRelation() { // Get the DataColumn objects from two DataTable objects // in a DataSet. Code to get the DataSet not shown here. DataColumn parentColumn = DataSet1.Tables["Customers"].Columns["CustID"]; DataColumn childColumn = DataSet1.Tables["Orders"].Columns["CustID"]; // Create DataRelation. DataRelation relCustOrder; relCustOrder = new DataRelation("CustomersOrders", parentColumn, childColumn); // Add the relation to the DataSet. DataSet1.Relations.Add(relCustOrder); }
(from http://msdn.microsoft.com/en-us/library/system.data.datarelation.aspx ) (来自http://msdn.microsoft.com/en-us/library/system.data.datarelation.aspx )
there will be a runtime error if there is orders that do not have customers. 如果订单没有客户,则会出现运行时错误。 This might happen when a buggy program has not deleted customer's orders when customer was deleted. 当有缺陷的程序在客户被删除时没有删除客户的订单时,可能会发生这种情况。
What can I do except put Orders select string a additional where-condition: 我可以做什么,除了将Orders选择字符串另外的where-condition:
CUSTID IN (SELECT DISTINCT CUSTID FROM CUSTOMERS)
OR: is it really that way (that all children have to have parents)? 或者:这是真的那样(所有孩子都必须有父母)? My code might have a bug also. 我的代码也可能有bug。 The exception occurs when IN MY CODE I add the relation to filled DataSet. 在IN MY CODE中我将关系添加到填充的DataSet时发生异常。 The exception is: 例外是:
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll System.Data.dll中发生了未处理的“System.ArgumentException”类型异常
Additional information: This constraint cannot be enabled as not all values have corresponding parent values. 附加信息:无法启用此约束,因为并非所有值都具有相应的父值。
Thanks & Best Regards - Matti 谢谢和最诚挚的问候 - 马蒂
there's an overload on the Add() method that includes a bool value to enforce constraints or not. Add()方法有一个重载,它包含一个bool值来强制执行约束。 I think I've used it in the past like this: 我想我过去曾经这样用过:
DataRelation dr = ds.Relations.Add("name", DataColumnParent, DataColumnChild, false);
EDIT 编辑
Sorry, the only overload is on this method signature: http://msdn.microsoft.com/en-us/library/3zy636kc.aspx 对不起,唯一的重载是这个方法签名: http : //msdn.microsoft.com/en-us/library/3zy636kc.aspx
DataRelationCollection.Add(String, DataColumn[], DataColumn[], bool)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.