页面加载的时候,在Table控件中加载TblStudent表中的内容

本文展示了如何在ASP.NET的动态网页中,利用C#从数据库TblStudent表中读取数据,并将内容加载到Table控件中显示。通过SqlConnection连接数据库,执行SQL查询所有记录,然后使用SqlDataReader遍历数据,动态创建表格行和单元格填充数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TblStudent表:


动态网页aspx前台:

<body>

    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" runat="server">
        </asp:Table>
    </div>
    </form>

</body>


动态网页后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


namespace demo3
{
    public partial class SelectAll : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string constr = "data source=.;initial catalog=School;User ID=sa;Password=111111";
            using (SqlConnection con = new SqlConnection(constr))
            {
                string sql = "select * from TblStudent";
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();//
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)//HasRows属性,可以判断这次查询有没有结果集返回。有为true,否则为false
                        {
                            while (reader.Read())
                            {     


                                TableRow trRow = new TableRow();
                                TableCell tcCell;
                                for (int i = 0; i < 8; i++)
                                {
                                    tcCell = new TableCell();
                                    tcCell.BorderStyle = BorderStyle.Solid;
                                    tcCell.BorderWidth = 1;
                                    tcCell.Text = reader.GetValue(i).ToString();
                                    trRow.Cells.Add(tcCell);
                                }
                                Table1.Rows.Add(trRow);


                                /*
                                TableRow trRow = new TableRow();
                                TableCell tcCell;
                                for (int i = 0; i < 8; i++)
                                {
                                    tcCell = new TableCell();
                                    tcCell.BorderStyle = BorderStyle.Solid;
                                    tcCell.BorderWidth = 1;
                                    tcCell.Text = reader.GetValue(i).ToString();
                                    trRow.Cells.Add(tcCell);
                                }
                                Table1.Rows.Add(trRow);
                                TableRow trRow = new TableRow();
                                //获取第一条记录的第一列中的值。
                                //reader.GetValue(0).ToString();
                                TableCell tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(0).ToString();
                                trRow.Cells.Add(tcCell);


                                //获取第一条记录的第2列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(1).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第3列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(2).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第4列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(3).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第5列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(4).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第6列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(5).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第7列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(6).ToString();
                                trRow.Cells.Add(tcCell);
                                //获取第一条记录的第8列中的值。
                                tcCell = new TableCell();
                                tcCell.BorderStyle = BorderStyle.Solid;
                                tcCell.BorderWidth = 1;
                                tcCell.Text = reader.GetValue(7).ToString();
                                trRow.Cells.Add(tcCell);
                                Table1.Rows.Add(trRow);*/

                            }
                        }
                    }
                }
            }
        }
    }
}


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值