我有以下函数只返回第一行数据并将其放在一个漂亮的表中。我在这方面的知识有限,因为我不知道如何创建表来返回多行。你能告诉我如何更改这个以返回所有行,无论有多少行?我的sql select语句可能返回0行或多行。
感谢:
<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
Public Shared Function GetDynamicContent(contextKey As String) As String
Dim constr As String = "Data Source=my data source"
Dim query As String = "SELECT * FROM Details WHERE ItemID = " & contextKey
Dim da As New SqlDataAdapter(query, constr)
Dim table As New DataTable()
da.Fill(table)
Dim b As New StringBuilder()
b.Append("<table style='background-color:#f3f3f3; border: #336699 3px solid; ")
b.Append("width:350px; font-size:10pt; font-family:Verdana;' cellspacing='0' cellpadding='3'>")
b.Append("<tr><td colspan='3' style='background-color:#336699; color:white;'>")
b.Append("<b>Budget Item Details</b>")
b.Append("</td></tr>")
b.Append("<tr><td style='width:80px;'><b>Detail</b></td>")
b.Append("<td style='width:80px;'><b>Amount</b></td>")
b.Append("<td><b>Date</b></td></tr>")
b.Append("<tr>")
b.Append("<td>$" & table.Rows(0)("Detail").ToString() & "</td>")
b.Append("<td>" & table.Rows(0)("Amount").ToString() & "</td>")
b.Append("<td>" & table.Rows(0)("DetailDate").ToString() & "</td>")
b.Append("</tr>")
b.Append("</table>")
Return b.ToString()
End Function
答案 0 :(得分:1)
试试这个。您需要FOR
循环来创建行。
Public Shared Function GetDynamicContent(contextKey As String) As String
Dim constr As String = "Data Source=my data source"
Dim query As String = "SELECT * FROM Details WHERE ItemID = " & contextKey
Dim da As New SqlDataAdapter(query, constr)
Dim table As New DataTable()
da.Fill(table)
Dim b As New StringBuilder()
b.Append("<table style='background-color:#f3f3f3; border: #336699 3px solid; ")
b.Append("width:350px; font-size:10pt; font-family:Verdana;' cellspacing='0' cellpadding='3'>")
b.Append("<table style='background-color:#f3f3f3; border: #336699 3px solid; ")
b.Append("width:350px; font-size:10pt; font-family:Verdana;' cellspacing='0' cellpadding='3'>")
b.Append("<tr><td colspan='3' style='background-color:#336699; color:white;'>")
b.Append("<b>Budget Item Details</b>")
b.Append("</td></tr>")
b.Append("<tr><td style='width:80px;'><b>Detail</b></td>")
b.Append("<td style='width:80px;'><b>Amount</b></td>")
b.Append("<td><b>Date</b></td></tr>")
For i As Integer = 0 To table.Rows.Count-1
b.Append("<tr>")
b.Append("<td>$" & table.Rows(i).Cell("Detail").ToString() & "</td>")
b.Append("<td>" & table.Rows(i).Cell("Amount").ToString() & "</td>")
b.Append("<td>" & table.Rows(i).Cell("DetailDate").ToString() & "</td>")
b.Append("</tr>")
Next
b.Append("</table>")
Return b.ToString()
End Function