我想将 datareader 数据分配到通用 List ( of T ) 中。
我不想阅读特定的列项目名称。我想让它动态化。
示例代码:
Shared Function FormatUserList(ByVal PoReader As OracleDataReader) As List(Of Users)
Dim oUserList As New List(Of Users)()
Dim oUser As Users
Dim dt As New DataTable
dt = PoReader.GetSchemaTable()
Do While PoReader.Read()
Dim index As Integer = 0
oUser = New Users()
oUser.LoginId = Convert.ToString(PoReader("LoginId"))
oUser.Password = Convert.ToString(PoReader("Password"))
oUser.FirstName = Convert.ToString(PoReader("FirstName"))
oUser.LastName = Convert.ToString(PoReader("LastName"))
oUser.NRIC = Convert.ToString(PoReader("NRIC"))
oUser.MobileNo = Convert.ToInt64(PoReader("MobileNo"))
oUser.Address = Convert.ToString(PoReader("Address"))
oUser.Zip = Convert.ToInt64(PoReader("Zip"))
oUser.State = Convert.ToInt64(PoReader("state"))
oUser.UserGroupId = Convert.ToInt64(PoReader("UserGroupId"))
oUser.CompanyId = Convert.ToInt64(PoReader("CompanyId"))
oUser.Active = Convert.ToInt64(PoReader("Active"))
oUserList.Add(oUser)
Loop
Return oUserList
End Function
我需要类似下面的东西,但是在将第二行数据循环到SetValue
.
“你调用的对象是空的。”
Do While PoReader.Read()
oUserGroup = New UserGroups()
Dim type As Type = GetType(UserGroups)
Dim obj As Object = Activator.CreateInstance(type)
For Each row As DataRow In dt.Rows
sColName = row.Field(Of String)("ColumnName")
type.GetProperty(sColName).SetValue(obj, PoReader(row.Field(Of String)("ColumnName")), Nothing)
oUserGroupList.Add(obj)
Next row
Loop