如何用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
相关产品推荐
相关产品推荐

