Is it possible to add a DataRelation to a DataSet if the child table contains rows that do not have a parent in the parent table in C # / ADO.NET 2.0

If I populate the DataSet with DataAdapters, which select all the rows from "Orders and Customers" and call:

  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 )

there will be a lead time error if there are orders that do not have customers. This can happen when the error did not delete the client orders when the client was deleted.

What can I do, except put orders selects an additional conditional line where:

  CUSTID IN (SELECT DISTINCT CUSTID FROM CUSTOMERS)

OR: is it true (that all children should have parents)? My code may have an error. An exception occurs when IN MY CODE I add a relation to a populated DataSet. The exception is:

An unhandled exception of type "System.ArgumentException" occurred in System.Data.dll

Additional information: This restriction cannot be included, because not all values โ€‹โ€‹have corresponding parent values.

Thanks and best regards - Matti

+4
source share
1 answer

There is an overload of the Add () method, which includes a bool value to enforce constraint restrictions. I think I used it in the past like this:

DataRelation dr = ds.Relations.Add("name", DataColumnParent, DataColumnChild, false); 

EDIT

Sorry, the only overload in this method signature is: http://msdn.microsoft.com/en-us/library/3zy636kc.aspx

 DataRelationCollection.Add(String, DataColumn[], DataColumn[], bool) 
+8
source

Source: https://habr.com/ru/post/1305122/


All Articles