类的属性怎么和DATAROW 的列关联
类的属性如何和DATAROW 的列关联
类的属性与DATAROW 的列关联是编程中经常大量用到的问题,比如从数据行中给类的属性赋值,或者反过来把属性的值通过数据行保存到数据库中.
如果是数据库应用程序,这种大量的赋值语句很麻烦,如果把这个过程自动后,编程就会简单一些了.各位有什么好的办法.来交流下拉.
我自己的办法基本上能实现这个功能,不过因为使用反射,效率低下,那位高手能不能给改进\优化一下:
属性的申明
Private f_项目用途 As String
<wyh_base.NetSys_Expand.ColumnName( "QG_XMYT ")> _
Public Property P_项目用途() As String
Get
Return Me.f_项目用途
End Get
Set(ByVal value As String)
Me.f_项目用途 = value
End Set
End Property
基类中自动赋值的方法
' ' ' <summary>
' ' ' 设置列名基本属性
' ' ' </summary>
' ' ' <remarks> </remarks>
Public Sub M_SetColNameBaseProperty()
'属性里面的列在查询处理类中找到别名后在数据行里面进行数据交换
If Me.P_BaseDataRow Is Nothing Then
Exit Sub
End If
Dim p1 As PropertyInfo
Dim p_AttType As Type = GetType(ColumnNameAttribute)
Dim p_colName As String '属性列名
Dim fact_colName As String '真实列名
For Each p1 In Me.GetType.GetProperties
p_colName = ColumnNameAttribute.GetPropertyColNameAttribute(p1)
fact_colName = Me.M_GetFactColName(p_colName)
If fact_colName <> " " Then
If Me.P_BaseDataRow.Table.Columns.Contains(fact_colName) Then
If Not Me.P_BaseDataRow.IsNull(fact_colName) Then
类的属性与DATAROW 的列关联是编程中经常大量用到的问题,比如从数据行中给类的属性赋值,或者反过来把属性的值通过数据行保存到数据库中.
如果是数据库应用程序,这种大量的赋值语句很麻烦,如果把这个过程自动后,编程就会简单一些了.各位有什么好的办法.来交流下拉.
我自己的办法基本上能实现这个功能,不过因为使用反射,效率低下,那位高手能不能给改进\优化一下:
属性的申明
Private f_项目用途 As String
<wyh_base.NetSys_Expand.ColumnName( "QG_XMYT ")> _
Public Property P_项目用途() As String
Get
Return Me.f_项目用途
End Get
Set(ByVal value As String)
Me.f_项目用途 = value
End Set
End Property
基类中自动赋值的方法
' ' ' <summary>
' ' ' 设置列名基本属性
' ' ' </summary>
' ' ' <remarks> </remarks>
Public Sub M_SetColNameBaseProperty()
'属性里面的列在查询处理类中找到别名后在数据行里面进行数据交换
If Me.P_BaseDataRow Is Nothing Then
Exit Sub
End If
Dim p1 As PropertyInfo
Dim p_AttType As Type = GetType(ColumnNameAttribute)
Dim p_colName As String '属性列名
Dim fact_colName As String '真实列名
For Each p1 In Me.GetType.GetProperties
p_colName = ColumnNameAttribute.GetPropertyColNameAttribute(p1)
fact_colName = Me.M_GetFactColName(p_colName)
If fact_colName <> " " Then
If Me.P_BaseDataRow.Table.Columns.Contains(fact_colName) Then
If Not Me.P_BaseDataRow.IsNull(fact_colName) Then