2

我今天试着做一些实验。我有一个使用无类型数据表作为模型实体的应用程序。它们都是这样制作的:

Imports System.Data
Imports System.Runtime.Serialization
Imports System.ComponentModel

<DesignerCategory("Code"), system.Serializable()>
Partial Public Class SomeTable1
    Inherits DataTable

#Region
    Public Const TABLE_NAME As String = "SomeTable1"

    Public Const FIELD_SomeField1 As String = "SomeField1"
    Public Const FIELD_SomeField2 As String = "SomeField2"
#End Region

    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub

    Public Sub New()
        MyBase.New()

        With Columns
            .Add(FIELD_SomeField1, GetType(System.String)).DefaultValue = String.Empty
            .Add(FIELD_SomeField2, GetType(System.Double)).DefaultValue = 0
        End With

        Dim keys(1) As DataColumn
        keys(0) = Columns(FIELD_SomeField1)
        TableName = TABLE_NAME
        PrimaryKey = keys
    End Sub
End Class

我目前正在使用 EF,所以在我的眼花缭乱中,我写了这样的东西(是的,它是 vb):

Partial Public Class SomeTable1
    Inherits DataTable

    <Key()>
    Friend Property SomePK1 As DataColumn

    <Required(ErrorMessage:="SomeField1 is required.")>
    <DataType(DataType.Text)>
    Friend Property SomeField1 As DataColumn

    <Required()>
    <DataType(DataType.DateTime)>
    Friend Property SomeField2 As DataColumn
    ...

    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub

    Public Sub New()
        MyBase.New()

        SomeField2 = Date.Now
    End Sub
End Class

我梦想做一些与以前的 dt 等效的东西,并与当前的数据引擎完全兼容。

然后类型转换错误(系统日期到数据列)打破了我的希望。我必须承认这是一个艰难的周末:)

因此,在我完全放弃更改之前,是否有任何方法可以编写类型化数据表,使其等同于上面的代码,但有一些新的好东西?这是一种非常古老的编程方式,我在网上找不到任何东西。提前致谢。

4

2 回答 2

0

不确定我是否完全遵循,但看起来您将 FIELD_SomeField2 定义为双精度

(第一个片段中的这一行)

.Add(FIELD_SomeField2, GetType(System.Double)).DefaultValue = 0

但是后来我看到您在第二个片段中将 SomeField2 定义为 DateTime 。

<Required()>
<DataType(DataType.DateTime)>
Friend Property SomeField2 As DataColumn

所以也许只是类型不匹配......

于 2013-03-05T17:58:05.850 回答
0

我找到了如何做我想做的事。也许涉及一些工作,但它确实有效。知道这是一种过时的做事方式,我在那里发帖,以便像我这样被迫维护旧程序的其他人可以受益。

执行类型化数据表的模板如下:

Imports System.Data
Imports System.ComponentModel
Imports System.Runtime.Serialization
Imports System.Diagnostics

'''<summary>
'''Represents the strongly named DataTable class.
'''</summary>
<Global.System.Serializable(), _
 Global.System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedTableSchema")> _
Partial Public Class tblMyTable
    Inherits TypedTableBase(Of tblMyTableRow)

    'Those are the StoredProcs names for (MANUAL) CRUD operations that the DBContext wrapper uses. (yuck! I hate thousands of them)
    'Public Const COMMAND_SAVE As String = "sp_MyTable_Save"
    'Public Const COMMAND_DELETE As String = "sp_MyTable_Delete"
    'Public Const COMMAND_LOADBY_ID As String = "sp_MyTable_LoadBy_Id"

    'Those are constants I maintain for untyped (but somewhat strong) compatibility
    Public Const FIELD_pID As String = "pID"
    Public Const FIELD_SomeOther As String = "SomeOtherField"

    'Basic CRUD, uses company data as the app hot swapps DBs (one for company)
    'Public Function Save(ByVal company As DataRow) As Short
    '    Return New Base(company).Update(Me, COMMAND_SAVE, COMMAND_DELETE)
    'End Function

    'Public Sub LoadByID(ByVal company As DataRow, Id As Integer)
    '    Me.Rows.Clear()
    '    Me.Merge(New Base(company).FillDataTable(Of tblMyTable)(COMMAND_LOADBY_ID, Id))
    'End Sub

    <DebuggerNonUserCodeAttribute()>
    Private Sub InitClass()
        Me.columnpID = New DataColumn(FIELD_pID, GetType(Integer), Nothing, MappingType.Element) With
                   {.AllowDBNull = False, .ReadOnly = True, .Unique = True,
                    .AutoIncrement = True, .AutoIncrementSeed = -1, .AutoIncrementStep = -1}
        MyBase.Columns.Add(Me.columnpID)
        Me.columnSomeOtherField = New DataColumn(FIELD_SomeOther, GetType(String), Nothing, MappingType.Element) With
                           {.MaxLength = 5, .AllowDBNull = False, .DefaultValue = String.Empty}
        MyBase.Columns.Add(Me.columnSomeOtherField)
    End Sub

    Private columnpID As DataColumn
    Private columnSomeOtherField As DataColumn

    <DebuggerNonUserCodeAttribute()>
    Public Sub New()
        MyBase.New()
        Me.TableName = "tblMyTable"
        Me.BeginInit()
        Me.InitClass()
        Me.EndInit()
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Friend Sub New(ByVal table As DataTable)
        MyBase.New()
        Me.TableName = table.TableName
        If (table.CaseSensitive <> table.DataSet.CaseSensitive) Then
            Me.CaseSensitive = table.CaseSensitive
        End If
        If (table.Locale.ToString <> table.DataSet.Locale.ToString) Then
            Me.Locale = table.Locale
        End If
        If (table.Namespace <> table.DataSet.Namespace) Then
            Me.Namespace = table.Namespace
        End If
        Me.Prefix = table.Prefix
        Me.MinimumCapacity = table.MinimumCapacity
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Protected Sub New(ByVal info As Global.System.Runtime.Serialization.SerializationInfo, ByVal context As Global.System.Runtime.Serialization.StreamingContext)
        MyBase.New(info, context)
        Me.InitVars()
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public ReadOnly Property pIDColumn() As DataColumn
        Get
            Return Me.columnpID
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public ReadOnly Property SomeOtherFieldColumn() As DataColumn
        Get
            Return Me.columnSomeOtherField
        End Get
    End Property

    <DebuggerNonUserCodeAttribute(), Browsable(False)>
    Public ReadOnly Property Count() As Integer
        Get
            Return Me.Rows.Count
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Default Public ReadOnly Property Item(ByVal index As Integer) As tblMyTableRow
        Get
            Return CType(Me.Rows(index), tblMyTableRow)
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public Overrides Function Clone() As DataTable
        Dim cln As tblMyTable = CType(MyBase.Clone, tblMyTable)
        cln.InitVars()
        Return cln
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function CreateInstance() As DataTable
        Return New tblMyTable()
    End Function

    <DebuggerNonUserCodeAttribute()>
    Friend Sub InitVars()
        Me.columnpID = MyBase.Columns(FIELD_pID)
        Me.columnSomeOtherField = MyBase.Columns(FIELD_SomeOther)
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public Function NewtblMyTableRow() As tblMyTableRow
        Return CType(Me.NewRow, tblMyTableRow)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function NewRowFromBuilder(ByVal builder As DataRowBuilder) As DataRow
        Return New tblMyTableRow(builder)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function GetRowType() As Global.System.Type
        Return GetType(tblMyTableRow)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Public Sub RemovetblMyTableRow(ByVal row As tblMyTableRow)
        Me.Rows.Remove(row)
    End Sub

End Class

'''<summary>
'''Represents strongly named DataRow class.
'''</summary>
Partial Public Class tblMyTableRow
    Inherits DataRow

    Private tabletblMyTable As tblMyTable

    <DebuggerNonUserCodeAttribute()>
    Friend Sub New(ByVal rb As DataRowBuilder)
        MyBase.New(rb)
        Me.tabletblMyTable = CType(Me.Table, tblMyTable)
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public Property pID() As Integer
        Get
            Return CType(Me(Me.tabletblMyTable.pIDColumn), Integer)
        End Get
        Set(value As Integer)
            Me(Me.tabletblMyTable.pIDColumn) = value
        End Set
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public Property SomeOtherField() As String
        Get
            Return CType(Me(Me.tabletblMyTable.SomeOtherFieldColumn), String)
        End Get
        Set(value As String)
            Me(Me.tabletblMyTable.SomeOtherFieldColumn) = value
        End Set
    End Property
End Class

这就是你所需要的。也许可以减少它,但是数据集函数将无法按预期工作。

如果您想要由 ID (VS2010) 为您自动生成的代码,您必须按照以下步骤操作:

  1. 在服务器资源管理器上,创建与您喜欢的数据库的连接
  2. 右键单击项目顶部并选择添加新元素。
  3. 只需选择数据集对象模板,名称无关紧要。它将在设计器视图中打开。
  4. 从数据库中选择表并拖动到数据集设计器。
  5. 然后...查看顶部的类选择器。
  6. 展开并找到 [yourTableName] 数据表。点击它。
  7. 它将跳转到 DataSet1.designer.vb (cs) 文件中的上述类。
  8. 下一个类是行定义。只需将它们复制粘贴到一个新的类文件中。
  9. 如果您想要一个更完整的数据表对象,则行类下面的下一个类定义事件,而委托它就在表定义的上方。

很简单,我测试它与使用无类型的剩余程序一起工作。也许这就像打磨粪便,但我想在某处添加数据注释以进行一些客户端验证,例如在 EF 中。并且可能为它们替换列构造函数参数。(但我不能)

祝你好运。

于 2013-03-07T17:28:03.743 回答