返回多个结果集:
2.使用mars:///
/// 返回多个结果集
///
///
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())
{
//.....绑定代码省略
}
}
}
///
/// 使用mars (multiple active resultsets,多活动结果集)
///
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();
}
}
}