您的位置:首页 > 其它

多个结果集的查询(使用table显示多个结果集)

2012-11-27 21:32 232 查看
前台:

<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());

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐