DataRelation 类

定义

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

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

示例

下面的示例创建一个新的 DataRelation ,并将其添加到的 DataRelationCollection DataSetThe 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 通过对象将两个对象相互关联 DataColumnA 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. 若要控制如何在子行中更改值,请将添加 ForeignKeyConstraintConstraintCollection 对象的 DataTableTo 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 属性 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 名称、父级和子级 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)

使用指定名称、父级和子级 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 名称、父级和子级 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)

使用指定的名称、父级和子级 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)

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

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

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

属性

ChildColumns

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

ChildKeyConstraint

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

ChildTable

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

DataSet

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

ExtendedProperties

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

Nested

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

ParentColumns

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

ParentKeyConstraint

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

ParentTable

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

RelationName

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

方法

CheckStateForProperty()

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

Equals(Object)

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

(继承自 Object)
GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
OnPropertyChanging(PropertyChangedEventArgs)

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

RaisePropertyChanging(String)

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

ToString()

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

适用于

线程安全性

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

另请参阅