前台:
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
</form>
</body>
后台:
protected void Page_Load(object sender, EventArgs e)
{
string constr = "data source=.;initial catalog=MySchool;User ID=sa;Password=111111";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select * from Student;select * from Class";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
do
{
if (reader.HasRows)
{
while (reader.Read())
{
TableRow trRow = new TableRow();
TableCell tcCell;
for (int i = 0; i < reader.FieldCount; 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);
}
}
} while (reader.NextResult());
}
}
}
}
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
</form>
</body>
后台:
protected void Page_Load(object sender, EventArgs e)
{
string constr = "data source=.;initial catalog=MySchool;User ID=sa;Password=111111";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select * from Student;select * from Class";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
do
{
if (reader.HasRows)
{
while (reader.Read())
{
TableRow trRow = new TableRow();
TableCell tcCell;
for (int i = 0; i < reader.FieldCount; 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);
}
}
} while (reader.NextResult());
}
}
}
}