[英]How to convert dataset object to List<string> in vb.net
我要从数据库返回行的结果集,我想在UI上为HTML控件分配这些值。
我正在从数据库获取数据到DataSet中。
例如,我的数据如下所示:
ID EmpId Question Comments
1 2 abcdefgh comments1
2 6 xyhgjkjh comments2
3 6 kjhkjhjk comments3
4 6 uyyiuyuui comments4
5 3 erteyeyuy comments5
6 6 qapooioip comments6
基于input(EmpId)我获取数据。对于EmpId = 6,我想使用如下HTML字段在UI上显示以上数据:
Q1。 xyhgjkjh
comments2
Q2。 kjhkjhjk
comments3
Q3.uyyiuyuui
comments4
Q4.qapooioip
comments6
我只想通过遍历数据库中的结果集以HTML标签显示。
我正在创建如下属性:
Public class MyClass
{
public int Id{get;set;}
public int EmpId{get;set;}
public String Question{get;set;}
public String Comments{get;set;}
}
我想使用上面的类,并将结果集值设置为属性,然后遍历它,并分配给UI上的HTML控件。
如何转换数据集并将其设置为MyClass obj属性,并将其分配给VB.NET中的HTML控件。
我需要具有“问题列表”属性吗?
有什么帮助吗? 提前致谢。
使用您的课程:
' I'm going to assume that the class properties are in the same order as the dataset and that there is only one table in the dataset.
Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyClassList as List(Of TheClassInstance)
Dim MyDataRow as DataRow
For Each MyDataRow in TheDataSet.Tables(0).Rows
TheClassInstance = New MyClass
TheClassInstance.Id = MyDataRow(0)
TheClassInstance.EmpId = MyDataRow(1)
TheClassInstance.Question = MyDataRow(2)
TheClassInstance.Comments = MyDataRow(3)
MyClassList.Add(TheClassInstance)
' Everytime you loop you're adding an instance of the class to the list
Next
要访问该类的每个实例,您可以使用for每个循环或使用变量作为索引的循环来循环,以获取列表中的数据。 对每个循环使用a:
Dim MyClassInstance as new MyClass
For Each MyClassInstance in MyClassList
' You can set the text property of labels and textbox controls to the data
Textbox_Comments.text = MyClassInstance.Comments
' ... etc
Next
编辑以解决每个父母的多重问题:
更改课程:
Public class MyClass
Public Property Id as Integer
Public Property EmpId as Integer
Public Property Question as List(of String)
Public Property Comments as String
End Class
我假设每个EmplID都有一个带有EmplId的父表和Questions的子表:
Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyDataRow as DataRow
Dim MyDataRow2 as DataRow ' For clarity
For Each MyDataRow IN ParentDataSet.Tables(0).Rows
TheClassInstance = New MyClass
TheClassInstance.Id = MyDataRow(0)
TheClassInstance.EmpId = MyDataRow(1)
TheClassInstance.Comments = MyDataRow(3)
Dim MySecondDataSet as Dataset = GetQuestions(MyDataRow(1) ' Get questions for EmplId
For Each MyDataRow2 in MySecondDataSet.Tables(0).Rows
' It's assumed that the question is the only column in the table
TheClassInstance.Questions.Add(MyDataRow2(0))
Next
Next
当所有问题都在表格的一行中时:
Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyDataRow as DataRow
For Each MyDataRow IN ParentDataSet.Tables(0).Rows
TheClassInstance = New MyClass
TheClassInstance.Id = MyDataRow(0)
TheClassInstance.EmpId = MyDataRow(1)
TheClassInstance.Comments = MyDataRow(3)
' I assume that the questions start in column 4
' Add each question in the row, I assume 4 questions in order
TheClassInstance.Questions.Add(MyDataRow(4))
TheClassInstance.Questions.Add(MyDataRow(5))
TheClassInstance.Questions.Add(MyDataRow(6))
TheClassInstance.Questions.Add(MyDataRow(7))
Next
创建将DataRow
转换为类MyClass
的方法的另一种方法
Public Function ToMyClass(row As DataRow)
Return New MyClass With
{
.Id = row.Field(Of Integer)("ID"),
.EmpId = row.Field(Of Integer)("EmpID"),
.Question = row.Field(Of String)("Question"),
.Comments = row.Field(Of String)("Comments"),
}
End Function
通用方法DataRow.Field(Of T)
将列值转换为期望的类型,这提供了编译器提供的某种类型安全性-为此,您需要将Option Strict
设置为On
。
然后,您将可以在“一行”中创建课程列表
Dim data As DataTable = dataset.Tables(0)
Dim questions As List(Of MyClass) =
data.AsEnumerable().Select(AddressOf ToMyClass).ToList()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.