问题描述
我想从数据表自定义.NET对象地图数据。比方说,我有一个.NET对象
I want to map data from datatable to custom .NET object. Let's say, I have a .NET object
Public Class MyClass
Public ID
Public Prop1
Public Prop2
End Class
和在数据库中,其中包含列的ID,为prop1和一个对应的表Prop2
And a corresponding table in the database, which contains columns ID, Prop1 and Prop2.
欲生成从数据库.NET对象的列表。目前,我填的是数据和每个属性单独映射。有什么办法,到数据库自动映射,所以性能会根据财产/列名映射。
I want to generate a list of .NET objects from the database. Currently I fill the dataset and map each property individually. Is there any way, to map the database automatically, so the properties would map according to property / column name.
'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
非常感谢您的回答。
Thank you very much for your answer.
推荐答案
有没有直接的方法,但很多工具可用来创建一个类似于究竟所谓的实体类的数据库表类。请参阅以下链接查看更多细节。
There is no direct way , although many tools are available to create classes that exactly resembles database tables called Entity classes. See the following link for more details
Database表到C#实体类 - 发电机FluentNHibernate
您也可以使用typeddataset,$ C $输入csmith工具。
You can also use typeddataset, codesmith tool.
这篇关于地图数据集在ASP.NET中的自定义.NET对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!