Vb.net:是否可以将sql select语句结果作为html表返回

时间:2014-03-25 16:38:14

标签: sql-server vb.net

这是我的代码

 Protected Sub Submit_Click(sender As Object, e As EventArgs)     
 Dim sSQL As String
 sSQL = "Select (ID),fromcur as 'From Currency',tocur as 'To Currency',rate as      'Conversion Rate',cast(convtimestamp as datetime) as 'Updated Time' 
 from Locations.dbo.Uni_Currency_Exchange_Rates 
 where fromCur = '" + FromCur.Text + "' and toCur = '" + ToCur.Text + "' order by ConvTimeStamp desc "
 Location2.SelectCommand = sSQL.ToString
 Dim command3 As New SqlCommand(sSQL, connection)

????????????????

End Sub

我需要生成html表代码 为每条记录创建行

<table>
<th>ID</th>
<th>From Currency</th>
<th>To Currency</th>
<th>Rate</th>
<th>Update Time</th>
<tr>1</tr>..............................
<tr>2</tr>..............................
.............................
</Table>

显示如下结果。

ID |来自货币|货币|率|更新时间

1 | MVR |美元| 15.42 | 3/25/14 12:00:00

1 | MVR | EUR | 22.00 | 3/25/14 12:00:00

有没有简短的方法来实现这一目标?

先谢谢

1 个答案:

答案 0 :(得分:1)

这很容易。有点旧学但我不知道有什么更容易的方法。

创建一个Table对象

Dim myTable as Table

在这里创建一个DataReader - 伪代码

Dim dr as DataReader = command.executeReader
while reader.Read()
TableRow r = new TableRow()
TableCell cell = new TableCell()
cell.Text = reader(0)
r.Cells.Add(cell)
myTable.Rows.Add(r)
next

Table也会公开标题行和标题单元格的对象。这可能是最简单的方法。 (您将为行中的每个列重复单元格创建和填充,将单元格添加到行中,然后将该行添加到表中)

除非您想使用XML,序列化和XSLT,否则我不知道有任何其他方法可以做到这一点......