无限分类

    技术2022-05-11  35

    ASP.NET无限分类

    字体大小: 小 中 大 <script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1962657149275668&dt=1178674165859&lmt=1178674165&format=728x90_as&output=html&channel=3842754409&url=http://www.leafweb.cn/article.asp?ID=1416&pages=2&color_bg=F4F4F4&color_text=000000&color_link=0000FF&color_url=008000&color_border=F4F4F4&ad_type=text_image&ref=http://www.google.cn/search?complete=1&hl=zh-CN&newwindow=1&q=asp.net+%E6%97%A0%E9%99%90%E5%88%86%E7%B1%BB&meta=&cc=100&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="allowtransparency"> 我参考上面的代码改成了asp.net版本的无限分类.开始遇到了语法上的限制.当时的处理是直接把rs换成SqlDataReader,然后加以修改,代码如下(错误代码): 测试数据库的表Tree结构是:id,ParentID,Name。  1 private   void  Display( string  parentid /*, int rank*/ )  2      { 3        SqlDataReader dr; 4        SqlCommand cmd; 5        String strSQL; 6 7        strSQL = "Select * From Tree Where ParentID =" + parentid + "Order By ID DESC"; 8        cmd = new SqlCommand(strSQL,conn); 9        //cmd.Connection.Open();1011        using(dr = cmd.ExecuteReader())12        {13            while(dr.Read())14            {15                Response.Write(dr["Name"].ToString() + "<br>");16                Display(dr["ID"].ToString());17            }18        }19        cmd.Connection.Close();20    } 调用使用Display("0"). 错误原因是SqlDataReader每次使用之后都需要关闭,所以DataReader是不可以嵌套使用的. 后来我改成了用DataTable的形式实现了无限分类,但是感觉这种方式的效率不高,需要改进(还在研究中) 修改后的代码如下:  1 private   void  Display( string  parentid, String space)  2      { 3        DataTable dt; 4        String strSQL; 5        strSQL = "Select * From Tree Where ParentID =" + parentid + " Order By ID DESC"; 6 7        SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn); 8        DataSet ds = new DataSet(); 9        sda.Fill(ds, "Tree");10        dt = ds.Tables["Tree"];1112        if (dt.Rows.Count > 0)13        {14            foreach (DataRow dr in dt.Rows)15            {16                strOpinion += space + "<font color=red>[" + dr["Name"].ToString() +"<br>";17                Display(dr["ID"].ToString(), "            " + space, false);18            }19        }20    } 调用时候使用Display("0","↓→→")。 虽然是实现了无限分类,但是效率还是挺低,努力探索更高的效率。 

    最新回复(0)