日期:2014-05-16  浏览次数:20587 次

数据库动态数据的处理+Repeater前台动态列的绑定

问题描述:

如图所示:数据库中有两张表,一张表可以根据课程id选出所有该课程的学生信息,另外一张表可以根据课程id选出该课程所有学生各次考试的成绩。由于考试次数是由老师自己动态设定的,事先并不清楚考试次数到底是多少。(.NET+Oracle网站实现)



1、首先,解决此类问题可以通过Repeater嵌套实现,对于数据量不大的时候可以这么用,但是一旦数据量稍大处理时间会变得很长,效率太差。

2、写个存储过程将所有数据整理成一张表,然后一次绑定到前台控件。

下面是该案例的存储过程:

create or replace procedure sp_get_all_cj(p_xkkh in varchar2, p_cursor out sys_refcursor)
   is
   sqlstr varchar2(32767);
   ct integer;
   begin

   sqlstr:='with cjj as ( select a.xh, b.xm, ';

   for v_cur in (select distinct id, ksmc,ksbh  from gcgl.ksxxb where xkkh = p_xkkh
     order by ksbh )
   loop
        sqlStr := sqlstr || 'sum(case ksid when '''|| v_cur.id ||''' then xscj else null end ) as c'|| v_cur.ksbh ||',';
   end loop;

   sqlstr := substr(sqlstr,0,length(sqlstr)-1) ||' from gcgl.xsksb a join gcgl.v_xsb b on a.xh = b.xh ' ||
         'where exists ( select * from gcgl.ksxxb b where a.ksid = b.id and b.xkkh = '''
         || p_xkkh || ''') group by a.xh, b.xm ) ' ;
         
         
   select count(*) into ct from gcgl.xszzcjb where xkkh = p_xkkh;
   
   if (ct >0) then--说明已经提交了
      sqlstr := sqlstr || ' select rank() over( order by b.xh) rowno, b.*, a.zpcj  '
         || ' from gcgl.xszzcjb a left join cjj b on a.xh = b.xh '
         || ' where a.xkkh = '''
         || p_xkkh 
         || ''' order by b.xh';
   else
       sqlstr := sqlstr || ' select rank() over( order by b.xh) rowno, b.*, 0 zpcj  '
         || ' from cjj b  order by b.xh';
   
   end if; 

   open p_cursor for sqlstr;

  end;

然后是以下C#语句,读出这张表,放到dataTable中

using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["JSCJLR"].ToString()))//连接字串
        {

            OleDbCommand myCMD = new OleDbCommand
             ("{call gcgl.sp_get_all_cj(?, {resultset 0, p_cursor})}", con);
            myCMD.Parameters.Add("p_xkkh", OleDbType.VarChar, 100).Value = xkkh;
            
            OleDbDataAdapter oda = new OleDbDataAdapter(myCMD);

            DataSet ds = new DataSet();

            oda.Fill(ds);
            dt = ds.Tables[0];

        }


读出的表结构大致是

xh,c1,c2,c3,c4...等,其中c1,,c2...是动态列,表示的是第几次考试


接下来的问题就是,有一个动态列的表,如何将该表绑定到前台控件中。

前台:

<table id="table" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">
    <caption> </caption>
    
    <asp:Repeater runat="server" ID="Repeater" OnItemDataBound=" rptClass1_ItemDataBound">
        <headerTemplate>   
            <asp:Literal ID="lit_head" runat="server"></asp:Literal>
        </headertemplate>   
        <itemtemplate>   
            <asp:Literal ID="lit_item" runat="server"></asp:Literal>
        </itemtemplate>   
    </asp:Repeater>
</table>

后台:

protected void rptClass1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        StringBuilder sb = n