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至的。 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

備註

是用來透過DataTable DataColumn物件使兩個物件彼此相互關聯。 DataRelationA 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請將ConstraintCollection新增至DataTable物件的。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屬性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 名稱以及父和子 DataRelation 物件,初始化 DataColumn 類別的新執行個體。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)

使用指定的名稱、父和子 DataRelation 物件,以及指出是否建立條件約束 (Constraint) 的值,初始化 DataColumn 類別的新執行個體。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 名稱以及父和子 DataRelation 物件的相符陣列,初始化 DataColumn 類別的新執行個體。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)

使用指定的名稱、父和子 DataRelation 物件的相符陣列,以及指出是否建立條件約束的值,初始化 DataColumn 類別的新執行個體。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

取得 DataSet 所屬的 DataRelationGets 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

取得為這個 DataColumn 父資料行的 DataRelation 物件陣列。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

取得這個 DataTableDataRelationGets the parent DataTable of this DataRelation.

RelationName RelationName RelationName RelationName

取得或設定用來從 DataRelation 擷取 DataRelationCollection 的名稱。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 的淺層複本 (Shallow Copy)。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.

另請參閱