惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
Y
Y Combinator Blog
V
V2EX
Engineering at Meta
Engineering at Meta
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
博客园 - 叶小钗
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
H
Help Net Security
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
B
Blog
G
Google Developers Blog
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
N
Netflix TechBlog - Medium
腾讯CDC

博客园 - Awen

asp.net插件实现 一些顶级的设计站点(陆续更新) asp.net定时任务实现(原创) Monorail的一些常用的东西(验证码,分页。。。持续更新) 基于Monorail的系统功能模块化 SubSonic:我想要的动态查询 SubSonic:亚音速之光 自娱自乐 3.5 -开篇有益 WinFormUI-Docking初级使用 一个喜欢网页设计的程序员 一个关于Excel的处理类(部分参考网络上) 抛开映射关系,不oo的数据访问写法 Javascript改观过程2 Javascript改观过程1 - Awen - 博客园 CEIMS项目笔记2:重复的,让工具做去! 备份&收集(持续更新)! CEIMS开发日记:项目计划 Jsp,.net! asp.net优化探讨系列(3)
一个顶N个的NextResult
Awen · 2008-04-19 · via 博客园 - Awen

    在大多数网站的开发中,很多功能都是模块化了的,方便统一和管理,用户控件显然是个不错的选择!我们常常会有很多栏目,封在用户控件里面,都是用来读取每个栏目的记录,每个控件的数据读取都是独立的,也就是说,这个页面有多少个这样的用户控件,就要建立多少个数据库连接,非常耗费资源!虽然用户控件可以用缓存,但是毕竟效率没有一次性读取的效率高!所以想了想,发现用DataReader的NextResult可以实现这样的效果!
首先是一个控件绑定的Helper:

 public class ListBinder
    
{
       
private List<Repeater> _controllist=new List<Repeater>();
        
public List<Repeater> Controllist
        
{
            
get return _controllist; }
        }

        
public  void BindAll(IDataReader dr)
        
{
            
int length=this._controllist.Count;
            
for (int i = 0; i < length;i++ )
            
{
                
if (i == 0)
                
{
                    _controllist[i].DataSource 
= dr;
                    _controllist[i].DataBind();
                }

                
else
                
{
                    
if (dr.NextResult())
                    
{
                        _controllist[i].DataSource 
= dr;
                        _controllist[i].DataBind();
                    }

                }

            }

        }

    }

接下来是测试调用的代码:

  string sql = "select top 5 * from [CaseShow] where [CategoryId]=9;select top 5 * from [CaseShow] where [CategoryId]=10;select top 5 * from [CaseShow] where [CategoryId]=11;select top 5 * from [CaseShow] where [CategoryId]=12";
            
using (IDbConnection con = AFrameWork.Data.DataFactory.GetConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            
{
                
try
                
{
                    IDbCommand cmd 
= con.CreateCommand();
                    cmd.CommandType 
= CommandType.Text;
                    cmd.CommandText 
= sql;
                    con.Open();
                    
using (IDataReader dr = cmd.ExecuteReader())
                    
{
                        ListBinder lb 
= new ListBinder();
                        lb.Controllist.Add(rpZhuanti);
                        lb.Controllist.Add(rpAd);
                        lb.Controllist.Add(rpHunli);
                        lb.Controllist.Add(rpPs);
                        lb.BindAll(dr);
                    }


                }

                
catch
                
{
                    con.Close();
                    
                    
throw;
                }

这里一次性读了四个栏目出来,只建立了一个数据库连接,比原来的效率和性能要好很多!不过这里对于结果集和控件的绑定对应就要自己控制了,当然我相信聪明的你会有办法搞定的,呵呵!