You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用SQL Server、VB.NET、ASP.NET实现数据库数据填充HTML表格

问题:ASP.NET门户网站中用SQL Server数据填充HTML表格

我在ASP.NET门户网站中搭建了一个基础HTML表格,代码如下:

<table ID="MasterTable" width="100%" style="border: 1px solid gray;"> 
      <tr>
        <th style="border: 1px solid gray;" width="33%">
          Promo Master ID
        </th>
        <th style="border: 1px solid gray;" width="33%">
          Name
        </th>
        <th style="border: 1px solid gray;" width="33%">
          PLU
        </th>
      </tr>

注:表格存在闭合标签,此处省略无关代码。

我希望将SQL Server数据库PromoMaster表中的PromoMasterID、Description、PLU数据填充到该表格中,目前已编写以下查询代码:

cmd = New SqlCommand("SELECT PromoMasterID, Description, PLU FROM PromoMaster")
cmd.Connection = conn

conn.Open()

请问如何通过这段代码实现表格数据填充?


更新:可行代码示例

感谢Albert D. Kallal的帮助,以下是可行代码:

ASP.NET部分

<asp:GridView ID="MasterGrid" runat="server" width="100%">
      <Columns>
      </Columns>
    </asp:GridView>

VB.NET部分

Protected Sub MasterGridFill()
    Dim MasterData As New DataTable
    Using cmdSQL As New SqlCommand("SELECT PromoMasterID, Description, PLU FROM PromoMaster ORDER BY PromoMasterID", conn)
        conn.Open()
        MasterData.Load(cmdSQL.ExecuteReader)
        conn.Close()
    End Using


    MasterGrid.DataSource = MasterData
    MasterGrid.DataBind()

End Sub

内容的提问来源于stack exchange,提问作者Alex Mo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:13:12