将class属性转换为datatable [英] convert class property to datatable
本文介绍了将class属性转换为datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个班级
公共班级PersonVO
继承ClientVO
Private _PersonFirstName As String
Private _PersonLastName As String
Private _PersonMiddleName As String
公共财产PersonFirstName()As String
获取
返回_PersonFirstName
结束获取
设置(ByVal值为字符串)
_PersonFirstName = value
结束集
结束财产
公共财产PersonLastName()As String
获取
返回_PersonLastName
结束获取
设置(ByVal值为字符串)
_PersonLastName =价值
结束套件
结束物业
Public Pro perty PersonMiddleName()As String
获取
返回_PersonMiddleName
结束获取
设置(ByVal值为字符串)
_PersonMiddleName = value
结束集
结束财产
结束班
我想转换为Datatable或Dataset,这样我就可以轻松保存在我的Sql中数据库
推荐答案
简短的回答是:使用ADO.NET。此CodeProject文章可以帮助您入门:为初学者使用ADO.NET [ ^ ]。
-SA
The short answer is: use ADO.NET. This CodeProject article can help you to get started: Using ADO.NET for beginners[^].
—SA
您无法在数据库中存储对象。
你可以序列化它。虽然更容易将类属性映射到数据库列然后保存它们。
You cannot store an object in your database.
YOu could serialize it. Though its easier to map class properties to database columns and then save them.
我找到了解决方案
--------- -----------
公共共享函数SetPersonDetails(ByVal PersonDetailsList As PersonVO)As DataTable
Dim dt As New DataTable()
试试
dt.TableName =人物
For Each [property] As PropertyInfo In PersonDetailsList。[GetType]()。GetProperties()
dt.Columns.Add(New DataColumn([property] .Name,[property] .PropertyType))
下一页
Dim newRow As DataRow = dt.NewRow()
For Each [property] As PropertyInfo In PersonDetailsList。[GetType]()。GetProperties()
newRow([property] .Name)= PersonDetailsList。[GetType]()。GetProperty([property] .Name) .GetValue(PersonDetailsList,Nothing)
下一页
dt.Rows.Add(newRow)
返回dt
Catch ex As Exception
Console.WriteLine (ex.ToString())
不返回
结束尝试
结束功能
I Found The Solution
--------------------
Public Shared Function SetPersonDetails(ByVal PersonDetailsList As PersonVO) As DataTable
Dim dt As New DataTable()
Try
dt.TableName = "Person"
For Each [property] As PropertyInfo In PersonDetailsList.[GetType]().GetProperties()
dt.Columns.Add(New DataColumn([property].Name, [property].PropertyType))
Next
Dim newRow As DataRow = dt.NewRow()
For Each [property] As PropertyInfo In PersonDetailsList.[GetType]().GetProperties()
newRow([property].Name) = PersonDetailsList.[GetType]().GetProperty([property].Name).GetValue(PersonDetailsList, Nothing)
Next
dt.Rows.Add(newRow)
Return dt
Catch ex As Exception
Console.WriteLine(ex.ToString())
Return Nothing
End Try
End Function
这篇关于将class属性转换为datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文