无法输入强类型数据表

时间:2013-03-05 17:47:21

标签: asp.net datatable strongly-typed-dataset

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

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合作,所以在我的ra语中,我写了这样的东西(是的,它是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

我梦想着制作与前者相同的东西,并与当前的数据引擎完全兼容。

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

所以在我完全放弃更改之前,有没有办法编写一个Typed数据表,所以它等同于上面的代码,但有一些新的好东西? 这是如此古老的编程方式,我在网上找不到任何东西。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

不确定我是否完全关注,但看起来你将FIELD_SomeField2定义为双

(第一个代码段中的这一行)

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

但后来我发现你在你的第二个片段中将SomeField2定义为DateTime。

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

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

答案 1 :(得分: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] Datatable。点击它。
  7. 它将跳转到DataSet1.designer.vb(cs)文件中的所述类。
  8. 下一课是行定义。只需将它们复制粘贴到新的类文件中即可。
  9. 如果你想要一个更完整的数据表对象,下面的下一课 行类定义事件,并且它位于上面的委托 table def。
  10. 简单,我测试它与剩余程序一起使用,使用无类型。 也许它就像抛光粪便一样,但我想在某处添加数据注释来做一些客户验证,比如EF。也许替换它们的列构造函数参数。 (但我可以&#39; t)

    祝你好运。