ADO.NET多活动结果集之NextResult方法
如果返回的是多个结果集,DataReader会提供NextResult方法来按顺序循环访问这些结果集。
示例如下:
String connStr = "Data Source=NIIT-QIU\\SQLEXPRESS;Initial catalog=MySchool;uid=sa;pwd=sa";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
String sql = "select classID,className from Class;select StudentId,LoginId from student";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader sdr=cmd.ExecuteReader();
do
{
//获取列名
Console.WriteLine("{0} {1}",sdr.GetName(0),sdr.GetName(1));
//输出列值
while (sdr.Read())
{
Console.WriteLine("{0}\t {1}",sdr.GetInt32(0),sdr.GetString(1));
}
Console.WriteLine("\n");
} while (sdr.NextResult());//进入下一个结果集
sdr.Close();
}
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
String sql = "select classID,className from Class;select StudentId,LoginId from student";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader sdr=cmd.ExecuteReader();
do
{
//获取列名
Console.WriteLine("{0} {1}",sdr.GetName(0),sdr.GetName(1));
//输出列值
while (sdr.Read())
{
Console.WriteLine("{0}\t {1}",sdr.GetInt32(0),sdr.GetString(1));
}
Console.WriteLine("\n");
} while (sdr.NextResult());//进入下一个结果集
sdr.Close();
}