【发布时间】:2012-05-24 00:07:24
【问题描述】:
我想将数据表中的数据映射到自定义 .NET 对象。假设我有一个 .NET 对象
Public Class MyClass
Public ID
Public Prop1
Public Prop2
End Class
以及数据库中对应的表,其中包含列ID、Prop1和Prop2。
我想从数据库中生成一个 .NET 对象列表。目前我填充数据集并单独映射每个属性。有什么办法可以自动映射数据库,所以属性会根据属性/列名进行映射。
'retreive dataset from db'
DaAdapter.Fill(ds)
'create a new list of my object'
Private l As New List(Of MyClass)
'iterate through dataset (this is the step I want to get rid of - of course'
'it could only work if the property of the MyClass and the column in '
'the database have the same name)'
For Each r As DataRow in ds.Tables(0).Rows
Dim itm As New MyClass
itm.ID = r("ID")
itm.Prop1 = r("Prop1")
itm.Prop2 = r("Prop2")
l.Add(itm)
Next
【问题讨论】:
标签: asp.net database object datatable mapping