1.返回多个结果集:
一个简单的查询可以返回多个结果集,在一次查询中返回多个结果集可以提高查询的效率,还能避免同时占用多个数据库链接
///
<summary>
/// 返回多个结果集
/// </summary>
/// <returns></returns>
public void GetTwoResultSet()
{
SqlConnection conn = new SqlConnection(_connstr);
string sql = " select * from Table_1;select * from Table_2 " ;
SqlCommand com = new SqlCommand(sql, conn);
using (conn)
{
conn.Open();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
// .....绑定代码省略
}
// 转到下一个结果集读取(重点就在此)
dr.NextResult();
while (dr.Read())
{
// .....绑定代码省略
}
}
}
/// 返回多个结果集
/// </summary>
/// <returns></returns>
public void GetTwoResultSet()
{
SqlConnection conn = new SqlConnection(_connstr);
string sql = " select * from Table_1;select * from Table_2 " ;
SqlCommand com = new SqlCommand(sql, conn);
using (conn)
{
conn.Open();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
// .....绑定代码省略
}
// 转到下一个结果集读取(重点就在此)
dr.NextResult();
while (dr.Read())
{
// .....绑定代码省略
}
}
}
2.使用MARS:
ADO.NET提供了一个名为MARS的新特性,在此版本之前数据库连接在有限的时间段内能且只能表示一个查询结果集。如果利用MARS特性,就可以使用单一的数据库连接来表示多个查询结果集。
默认情况下MARS特性是被关闭的,要开启该功能,只需要在数据库连接字符串中加上一个字段属性:MultipleActiveResultsets=True
///
<summary>
/// 使用MARS (Multiple Active Resultsets,多活动结果集)
/// </summary>
public void GetByMARS()
{
// MultipleActiveResultsets=True; 开启sql的MARS
string connstr = @" MultipleActiveResultsets=True;DataSource=.\SQLExpress;Integrated Security=True; " ;
connstr += @" AttachDBFileName=|DataDirectory|Mydatabase.mdf;User Instance=True " ;
SqlConnection conn = new SqlConnection(connstr);
string sqlCata = " select * from Catagory " ;
SqlCommand cmdCata = new SqlCommand(sqlCata, conn);
string sql = " select * from Movies where CatagoryID = @CatagoryID " ;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add( " @CatagoryID " , SqlDbType.Int);
using (conn)
{
conn.Open();
SqlDataReader drCata = cmdCata.ExecuteReader();
while (drCata.Read())
{
int cataid = int .Parse(drCata[ " CatagoryID " ].ToString());
cmd.Parameters[ " @CatagoryID " ].Value = cataid;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
// .....绑定代码省略
}
dr.Close();
}
}
}
/// 使用MARS (Multiple Active Resultsets,多活动结果集)
/// </summary>
public void GetByMARS()
{
// MultipleActiveResultsets=True; 开启sql的MARS
string connstr = @" MultipleActiveResultsets=True;DataSource=.\SQLExpress;Integrated Security=True; " ;
connstr += @" AttachDBFileName=|DataDirectory|Mydatabase.mdf;User Instance=True " ;
SqlConnection conn = new SqlConnection(connstr);
string sqlCata = " select * from Catagory " ;
SqlCommand cmdCata = new SqlCommand(sqlCata, conn);
string sql = " select * from Movies where CatagoryID = @CatagoryID " ;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add( " @CatagoryID " , SqlDbType.Int);
using (conn)
{
conn.Open();
SqlDataReader drCata = cmdCata.ExecuteReader();
while (drCata.Read())
{
int cataid = int .Parse(drCata[ " CatagoryID " ].ToString());
cmd.Parameters[ " @CatagoryID " ].Value = cataid;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
// .....绑定代码省略
}
dr.Close();
}
}
}