Export (0) Print
Expand All

DataRelation Class

Represents a parent/child relationship between two DataTable objects.

System.Object
  System.Data.DataRelation

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public class DataRelation

The DataRelation type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkDataRelation(String, DataColumn, DataColumn)Initializes a new instance of the DataRelation class using the specified DataRelation name, and parent and child DataColumn objects.
Public methodSupported by the XNA FrameworkDataRelation(String, DataColumn[], DataColumn[])Initializes a new instance of the DataRelation class using the specified DataRelation name and matched arrays of parent and child DataColumn objects.
Public methodSupported by the XNA FrameworkDataRelation(String, DataColumn, DataColumn, Boolean)Initializes a new instance of the DataRelation class using the specified name, parent and child DataColumn objects, and a value that indicates whether to create constraints.
Public methodSupported by the XNA FrameworkDataRelation(String, DataColumn[], DataColumn[], Boolean)Initializes a new instance of the DataRelation class using the specified name, matched arrays of parent and child DataColumn objects, and value that indicates whether to create constraints.
Public methodSupported by the XNA FrameworkDataRelation(String, String, String, String[], String[], Boolean)This constructor is provided for design time support in the Visual Studio environment.
Public methodDataRelation(String, String, String, String, String, String[], String[], Boolean)This constructor is provided for design time support in the Visual Studio environment.
Top

  NameDescription
Public propertySupported by the XNA FrameworkChildColumnsGets the child DataColumn objects of this relation.
Public propertySupported by the XNA FrameworkChildKeyConstraintGets the ForeignKeyConstraint for the relation.
Public propertySupported by the XNA FrameworkChildTableGets the child table of this relation.
Public propertySupported by the XNA FrameworkDataSetGets the DataSet to which the DataRelation belongs.
Public propertySupported by the XNA FrameworkExtendedPropertiesGets the collection that stores customized properties.
Public propertySupported by the XNA FrameworkNestedGets or sets a value that indicates whether DataRelation objects are nested.
Public propertySupported by the XNA FrameworkParentColumnsGets an array of DataColumn objects that are the parent columns of this DataRelation.
Public propertySupported by the XNA FrameworkParentKeyConstraintGets the UniqueConstraint that guarantees that values in the parent column of a DataRelation are unique.
Public propertySupported by the XNA FrameworkParentTableGets the parent DataTable of this DataRelation.
Public propertySupported by the XNA FrameworkRelationNameGets or sets the name used to retrieve a DataRelation from the DataRelationCollection.
Top

  NameDescription
Protected methodSupported by the XNA FrameworkCheckStateForPropertyInfrastructure. This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkOnPropertyChangingThis member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Protected methodSupported by the XNA FrameworkRaisePropertyChangingThis member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Public methodSupported by the XNA FrameworkToStringGets the RelationName, if one exists. (Overrides Object.ToString().)
Top

A DataRelation is used to relate two DataTable objects to each other through DataColumn objects. For example, in a Customer/Orders relationship, the Customers table is the parent and the Orders table is the child of the relationship. This is similar to a primary key/foreign key relationship. For more information, see Navigating DataRelations.

Relationships are created between matching columns in the parent and child tables. That is, the DataType value for both columns must be identical.

Relationships can also cascade various changes from the parent DataRow to its child rows. To control how values are changed in child rows, add a ForeignKeyConstraint to the ConstraintCollection of the DataTable object. The ConstraintCollection determines what action to take when a value in a parent table is deleted or updated.

When a DataRelation is created, it first verifies that the relationship can be established. After it is added to the DataRelationCollection, the relationship is maintained by disallowing any changes that would invalidate it. Between the period when a DataRelation is created and added to the DataRelationCollection, it is possible for additional changes to be made to the parent or child rows. An exception is generated if this causes a relationship that is no longer valid.

NoteNote

Data corruption can occur if a bi-directional relation is defined between two tables. A bi-directional relation consists of two DataRelation objects that use the same columns, with the parent-child roles switched. No exception is raised when the DataRelation objects are saved; however, data corruption can occur.

DataRelation objects are contained in a DataRelationCollection, which you can access through the Relations property of the DataSet, and the ChildRelations and ParentRelations properties of the DataTable.

The following example creates a new DataRelation and adds it to the DataRelationCollection of a DataSet.

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);
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is safe for multithreaded read operations. You must synchronize any write operations.

Show:
© 2014 Microsoft