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的, 并将其DataRelationCollection添加到DataSet的。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);
}
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. 例如, 在客户/订单关系中, Customers 表是父表, 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. 若要控制如何在子行中更改值, 请ForeignKeyConstraint将添加ConstraintCollectionDataTable对象的。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.

创建时, 它首先验证是否可以建立关系。DataRelationWhen 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属性和ChildRelations的和ParentRelations属性DataTable访问这些对象。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.

构造函数

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.

另请参阅