public partial class 列循环 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string constr = "data source=PC-20120604KNHD;initial catalog=Schools;user id=sa;password=910809";
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select * from T_Student;select * from TblClass";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
#region//读取一个结果集
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Response.Write(reader.GetValue(i));
}
Response.Write("<br>");
}
#endregion
#region//读取多个结果集
//do------while ,do中先来读取一个结果集,while(reader.NextResult())中判断是否还有结果集,如果有do中再次读取第二个结果集,依此类推...
do
{
if (reader.HasRows)
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Response.Write(reader.GetValue(i).ToString());
}
Response.Write("<br>");
}
}
}
while (reader.NextResult());
#endregion
}
}
}
}
}
}