DataRelation DataRelation DataRelation DataRelation Class

定义

表示两个 DataTable 对象之间的父/子关系。Represents a parent/child relationship between two DataTable objects.

public ref class DataRelation
[System.ComponentModel.TypeConverter(typeof(System.Data.RelationshipConverter))]
[System.Serializable]
public class DataRelation
type DataRelation = class
Public Class DataRelation
继承
DataRelationDataRelationDataRelationDataRelation
属性

示例

下面的示例创建一个新DataRelation并将其添加到DataRelationCollectionDataSetThe 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);
}
Private Sub CreateRelation()
    ' Get the DataColumn objects from two DataTable objects 
    ' in a DataSet. Code to get the DataSet not shown here.
    Dim parentColumn As DataColumn = _
        DataSet1.Tables("Customers").Columns("CustID")
    Dim childColumn As DataColumn = DataSet1.Tables( _
        "Orders").Columns("CustID")

    ' Create DataRelation.
    Dim relCustOrder As DataRelation
    relCustOrder = New DataRelation( _
        "CustomersOrders", parentColumn, childColumn)

    ' Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder)
End Sub

注解

一个DataRelation用于使两个相关DataTable通过每个其他对象DataColumn对象。A DataRelation is used to relate two DataTable objects to each other through DataColumn objects. 例如,在客户/订单关系中,客户表为父表和 Orders 表是关系的子级。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. 有关详细信息,请参阅导航 DatarelationFor more information, see Navigating DataRelations.

创建父和子表中的匹配列之间的关系。Relationships are created between matching columns in the parent and child tables. 也就是说,DataType的这两列必须为相同值。That is, the DataType value for both columns must be identical.

关系还可以级联从父对象的各种更改DataRow到其的子行。Relationships can also cascade various changes from the parent DataRow to its child rows. 若要控制如何在子行中更改值,添加ForeignKeyConstraintConstraintCollectionDataTable对象。To control how values are changed in child rows, add a ForeignKeyConstraint to the ConstraintCollection of the DataTable object. ConstraintCollection确定要在父表中的值时采取的操作是删除或更新。The ConstraintCollection determines what action to take when a value in a parent table is deleted or updated.

DataRelation是创建的它会首先验证可以建立关系。When a DataRelation is created, it first verifies that the relationship can be established. 添加到后DataRelationCollection,而不允许任何更改会使其失效的维护的关系。After it is added to the DataRelationCollection, the relationship is maintained by disallowing any changes that would invalidate it. 之间的这段时DataRelation被创建并添加到DataRelationCollection,很可能要对父或子行进行的其他更改。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.

备注

如果两个表之间定义的双向关系,则会发生数据损坏。Data corruption can occur if a bi-directional relation is defined between two tables. 双向关系包含两个DataRelation相同的列,用于切换的父-子角色的对象。A bi-directional relation consists of two DataRelation objects that use the same columns, with the parent-child roles switched. 不会引发异常时DataRelation保存对象; 但是,会发生数据损坏。No exception is raised when the DataRelation objects are saved; however, data corruption can occur.

DataRelation 对象包含在DataRelationCollection,使用户能够通过Relations的属性DataSet,和ChildRelationsParentRelations属性的DataTableDataRelation 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.

构造函数

DataRelation(String, DataColumn, DataColumn) DataRelation(String, DataColumn, DataColumn) DataRelation(String, DataColumn, DataColumn) DataRelation(String, DataColumn, DataColumn)

使用指定的 DataRelation 名称、父级和子级 DataColumn 对象,初始化 DataRelation 类的新实例。Initializes a new instance of the DataRelation class using the specified DataRelation name, and parent and child DataColumn objects.

DataRelation(String, DataColumn, DataColumn, Boolean) DataRelation(String, DataColumn, DataColumn, Boolean) DataRelation(String, DataColumn, DataColumn, Boolean) DataRelation(String, DataColumn, DataColumn, Boolean)

使用指定名称、父级和子级 DataColumn 对象以及指示是否创建约束的值,初始化 DataRelation 类的新实例。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.

DataRelation(String, DataColumn[], DataColumn[]) DataRelation(String, DataColumn[], DataColumn[]) DataRelation(String, DataColumn[], DataColumn[]) DataRelation(String, DataColumn[], DataColumn[])

使用指定的 DataRelation 名称、父级和子级 DataColumn 对象的匹配数组,初始化 DataRelation 类的新实例。Initializes a new instance of the DataRelation class using the specified DataRelation name and matched arrays of parent and child DataColumn objects.

DataRelation(String, DataColumn[], DataColumn[], Boolean) DataRelation(String, DataColumn[], DataColumn[], Boolean) DataRelation(String, DataColumn[], DataColumn[], Boolean) DataRelation(String, DataColumn[], DataColumn[], Boolean)

使用指定的名称、父级和子级 DataColumn 对象的匹配数组以及指示是否创建约束的值,初始化 DataRelation 类的新实例。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.

DataRelation(String, String, String, String, String, String[], String[], Boolean) DataRelation(String, String, String, String, String, String[], String[], Boolean) DataRelation(String, String, String, String, String, String[], String[], Boolean) DataRelation(String, String, String, String, String, String[], String[], Boolean)

此构造函数在 Visual Studio 环境中提供设计时支持。This constructor is provided for design time support in the Visual Studio environment.

DataRelation(String, String, String, String[], String[], Boolean) DataRelation(String, String, String, String[], String[], Boolean) DataRelation(String, String, String, String[], String[], Boolean) DataRelation(String, String, String, String[], String[], Boolean)

此构造函数在 Visual Studio 环境中提供设计时支持。This constructor is provided for design time support in the Visual Studio environment.

属性

ChildColumns ChildColumns ChildColumns ChildColumns

获取此关系的子级 DataColumn 对象。Gets the child DataColumn objects of this relation.

ChildKeyConstraint ChildKeyConstraint ChildKeyConstraint ChildKeyConstraint

获取关系的 ForeignKeyConstraintGets the ForeignKeyConstraint for the relation.

ChildTable ChildTable ChildTable ChildTable

获取此关系的子表。Gets the child table of this relation.

DataSet DataSet DataSet DataSet

获取 DataRelation 所属的 DataSetGets the DataSet to which the DataRelation belongs.

ExtendedProperties ExtendedProperties ExtendedProperties ExtendedProperties

获取存储自定义属性的集合。Gets the collection that stores customized properties.

Nested Nested Nested Nested

获取或设置一个指示是否嵌套 DataRelation 对象的值。Gets or sets a value that indicates whether DataRelation objects are nested.

ParentColumns ParentColumns ParentColumns ParentColumns

获取作为此 DataRelation 的父列的 DataColumn 对象的数组。Gets an array of DataColumn objects that are the parent columns of this DataRelation.

ParentKeyConstraint ParentKeyConstraint ParentKeyConstraint ParentKeyConstraint

获取 UniqueConstraint,它确保 DataRelation 的父列中的值是唯一的。Gets the UniqueConstraint that guarantees that values in the parent column of a DataRelation are unique.

ParentTable ParentTable ParentTable ParentTable

获取此 DataRelation 的父级 DataTableGets the parent DataTable of this DataRelation.

RelationName RelationName RelationName RelationName

获取或设置用于从 DataRelationCollection 中检索 DataRelation 的名称。Gets or sets the name used to retrieve a DataRelation from the DataRelationCollection.

方法

CheckStateForProperty() CheckStateForProperty() CheckStateForProperty() CheckStateForProperty()

此方法支持 .NET Framework 基础结构,但不适合直接在代码中使用。This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs)

此成员支持 .NET Framework 结构,不能在代码中直接使用。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

RaisePropertyChanging(String) RaisePropertyChanging(String) RaisePropertyChanging(String) RaisePropertyChanging(String)

此成员支持 .NET Framework 结构,不能在代码中直接使用。This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

ToString() ToString() ToString() ToString()

获取 RelationName(如果存在)。Gets the RelationName, if one exists.

适用于

线程安全性

此类型是安全的多线程读取操作。This type is safe for multithreaded read operations. 你必须同步的任何写入操作。You must synchronize any write operations.

另请参阅