0

我有以下函数,它只返回第一行数据并将其放在一个漂亮的表中。我在这方面的知识有限,因为我不知道如何创建表以返回多行。你能告诉我如何改变它以返回所有行,不管有多少行?我的 sql 选择语句可能返回 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
4

1 回答 1

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
于 2012-05-17T00:30:40.440 回答