我找到了如何做我想做的事。也许涉及一些工作,但它确实有效。知道这是一种过时的做事方式,我在那里发帖,以便像我这样被迫维护旧程序的其他人可以受益。
执行类型化数据表的模板如下:
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) 为您自动生成的代码,您必须按照以下步骤操作:
- 在服务器资源管理器上,创建与您喜欢的数据库的连接
- 右键单击项目顶部并选择添加新元素。
- 只需选择数据集对象模板,名称无关紧要。它将在设计器视图中打开。
- 从数据库中选择表并拖动到数据集设计器。
- 然后...查看顶部的类选择器。
- 展开并找到 [yourTableName] 数据表。点击它。
- 它将跳转到 DataSet1.designer.vb (cs) 文件中的上述类。
- 下一个类是行定义。只需将它们复制粘贴到一个新的类文件中。
- 如果您想要一个更完整的数据表对象,则行类下面的下一个类定义事件,而委托它就在表定义的上方。
很简单,我测试它与使用无类型的剩余程序一起工作。也许这就像打磨粪便,但我想在某处添加数据注释以进行一些客户端验证,例如在 EF 中。并且可能为它们替换列构造函数参数。(但我不能)
祝你好运。