论坛首页 编程语言技术论坛

html表格显示数据库中多对多关系的思考

浏览 2208 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-08-05  
select * from v_coll_port where

collid in (select collid from v_coll_port where pollid=@pollID

group by collid having count(collid) > 1)

select * from V_coll_port where portid in(

select portid from v_coll_port where pollid=@pollID

group by portid having count(portID)>1)

select * from v_coll_port where pollid=@pollID and collid in (

select collid from v_coll_port group by collid having count(collid) = 1

) and portID in (

select portID from v_coll_port group by portID having count(portID) = 1

)
 



以上代码造出一对多、多对一和一对一的三种关系并列表,在后台得到表格后做如下处理

DataTable dt12N = ds.Tables[0];
            DataTable dtn21 = ds.Tables[1];
            DataTable dt121 = ds.Tables[2];
            GridNotStyle g = new GridNotStyle(2,"black","white","white");
            for(int i=0;i<dt12N.Rows.Count;i++)
            {
                DataRow r = dt12N.Rows[i];
                if(i==0)
                {
                    g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString()+"^r^"+dt12N.Rows.Count,
                        this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString());
                }
                else
                {
                    g.SetHead(this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString());
                }
            }
            for(int i=0;i<dtn21.Rows.Count;i++)
            {
                DataRow r = dtn21.Rows[i];
                if(i==0)
                {
                    g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString(),
                        this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString()+"^r^"+dtn21.Rows.Count);
                }
                else
                {
                    g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString());
                }
            }
            foreach(DataRow r in dt121.Rows)
            {
                g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString(),
                    this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString());
            }
 



只是这样的方法只能处理一对多、多对一和一对一都单独存在的情况,倘若是向错的多对多,还真是没有想到更好的办法 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics