gpt4 book ai didi

vb.net - .Net DataSet 不适合设计时

转载 作者:行者123 更新时间:2023-12-01 15:08:49 24 4
gpt4 key购买 nike

我写了一个自定义数据集(基于强类型数据集,我进一步了解)我想让它适合在设计时使用,比如从工具箱中拖拽(作为数据集工具自动生成) .所有这一切都在发生,因为我试图保留我的同事设定的开发方法(我是新来的,哈哈哈),当然我知道 Entity Framework 的存在和类似的东西。因此,当我将它放在我的表单上时出现此错误:

Relation named 'ParentChild_Relation' already belongs to this DataSet

当然是物质关系

这是代码:

Public Class MyDataSet
Inherits DataSet

Public Const PARENTCHILD_RELATION As String = "ParentChild_Relation"

Public Sub New()
MyBase.New()
Me.BeginInit()
Me.CreateDataSet()
Me.EndInit()
End Sub

Private Sub CreateDataSet()
DataSetName = "Test"
Me.EnforceConstraints = True
Me.Tables.Add(New ParentTable) 'custom datatable
Me.Tables.Add(New ChildTable) 'custom datatable

CreateRelation()
End Sub

Private Sub CreateRelation()
With DirectCast(Me.Tables(ChildTable.TABLENAME).Constraints.Add(PARENTCHILD_RELATION ,
Me.Tables(ParentTable.TABLENAME).Columns(ParentTable.ID),
Me.Tables(ChildTable.TABLENAME).Columns(ChildTable.ID)), ForeignKeyConstraint)
.UpdateRule = Rule.Cascade
.DeleteRule = Rule.Cascade
Me.Relations.Add(.ConstraintName, .RelatedColumns, .Columns, False)
End With
End Sub

End Class

我会知道任何可能的帮助

编辑

没有这一行也能工作:

Me.Relations.Add(.ConstraintName, .RelatedColumns, .Columns, False)

但是,我仍然想要 DataSet 关系,因为这是它们的工作方式

编辑 2

没有上一行,它不会通过绑定(bind)找到关系:

BindingSource.DataSource = New MyDataSet
BindingSource.DataMember = "ParentChild_Relation"

最佳答案

我必须这样做:实现 DataSet.Initialized 事件,然后在其中创建所有关系。

Public Class MyDataSet
Inherits DataSet

Public Const PARENTCHILD_RELATION As String = "ParentChild_Relation"

Public Sub New()
MyBase.New()
Me.CreateDataSet()
With DirectCast(Me.Tables(ChildTable.TABLENAME).Constraints.Add(PARENTCHILD_RELATION ,
Me.Tables(ParentTable.TABLENAME).Columns(ParentTable.ID),
Me.Tables(ChildTable.TABLENAME).Columns(ChildTable.ID)), ForeignKeyConstraint)
.UpdateRule = Rule.Cascade
.DeleteRule = Rule.Cascade
End With
AddHandler Initialized, AddressOf InitializedHandler
End Sub


Private Sub InitializedHandler(sender As Object, e As EventArgs)
Relations.AddRange((From tb As DataTable In Tables
From fkc As ForeignKeyConstraint In tb.Constraints.OfType(Of ForeignKeyConstraint)()
Where Not Relations.Contains(fkc.ConstraintName)
Select New DataRelation(fkc.ConstraintName, fkc.RelatedColumns, fkc.Columns, False)).ToArray())
End Sub

Private Sub CreateDataSet()
DataSetName = "Test"
Me.EnforceConstraints = True
Me.Tables.Add(New ParentTable) 'custom datatable
Me.Tables.Add(New ChildTable) 'custom datatable
End Sub

End Class

我认为这对其他人有帮助

关于vb.net - .Net DataSet 不适合设计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36606183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com