步骤一.返回多个查询集合,方法就多了,如用存储返回多个查询;或直接在ASP.NET写SQL命令;或者用多个数据源控件,分别绑定不同的数据控件如gridview;或者用同一个adapter,每次查询一个表,并存入ds的同名表中,查询完成后再更换新的selectcommand.commText,查询新表,如此反复.存储返回多个查询集: select top 10 title from table where cata=1 select top 10 title from table where cata=2 select top 10 title from table where cata=3 select top 10 title from table where cata=4 或者 SQL="select top 10 title from table where cata=1;select top 10 title from table where cata=2;select top 10 title from table where cata=3;select top 10 title from table where cata=4".....//初始化conn SqlDataAdapter ad=new SqlDataAdapter(sql,conn); DataSet ds=new dataset(); ad.fill(ds);现在就返回了多个表,你可以将不同表的defaultview分别绑定不同的gridview,或者循环手工输出代码for each(DataRow row in ds.Tables["table"].Rows) { 循环输出类1 }foreach(row in ds.Tables["table1"].Rows) { 输出类2 }foreach(row in ds.Tables["table2"].Rows) { 输出类3 }当然,我们还可以用datareader+NextTesult来操作多个结果集.总结: 1. 用数据源控件+数据控件,最简单 2. 用ado.net取得多个结果集,然后绑定数据控件,中等简单 3. 用ado.net取得多个结果集,并手工输出到页面.复杂 要取得多个结果集: a 可以用存储 b 或者直接在SQL命令中用;号隔开多个查询命令 c asp.net与数据库来回操作多次查询
select top 10 title from table where cata=1
select top 10 title from table where cata=2
select top 10 title from table where cata=3
select top 10 title from table where cata=4
或者
SQL="select top 10 title from table where cata=1;select top 10 title from table where cata=2;select top 10 title from table where cata=3;select top 10 title from table where cata=4".....//初始化conn
SqlDataAdapter ad=new SqlDataAdapter(sql,conn);
DataSet ds=new dataset();
ad.fill(ds);现在就返回了多个表,你可以将不同表的defaultview分别绑定不同的gridview,或者循环手工输出代码for each(DataRow row in ds.Tables["table"].Rows)
{
循环输出类1
}foreach(row in ds.Tables["table1"].Rows)
{
输出类2
}foreach(row in ds.Tables["table2"].Rows)
{
输出类3
}当然,我们还可以用datareader+NextTesult来操作多个结果集.总结:
1. 用数据源控件+数据控件,最简单
2. 用ado.net取得多个结果集,然后绑定数据控件,中等简单
3. 用ado.net取得多个结果集,并手工输出到页面.复杂
要取得多个结果集:
a 可以用存储
b 或者直接在SQL命令中用;号隔开多个查询命令
c asp.net与数据库来回操作多次查询
可以用datareader+nextresult(),也可以用adapter+dataset