C# 用循环来遍历无限级分类

    //为下拉框填充数据无限级分类,用循环完成     public void DropClass()     {  //效率太低,已经不用了      

 string sql, ids, f_id, str;        

 int tempi, i, j;        

tempi = 0;         ids = "-1";         str = "";         sql = "select * from News_type where fid=0  order by fid,sortnum";         SqlDataReader oRs, Rs, Mys, frs;         Rs = MyData.ExecuteReader(sql);         i = 0;         while (Rs.Read())         {

            News_lb.Items.Insert(i, new ListItem(MyData.DelHTML(Rs["ClassName"].ToString()), Rs["id"].ToString()));             i = i + 1;             f_id = Rs["id"].ToString();             ids = ids + "," + Rs["id"].ToString();             do             {                 sql = "select * from News_type where fid=" + f_id + " and id not in(" + ids + ")  order by fid,sortnum";                 oRs = MyData.ExecuteReader(sql);

                if (!oRs.Read())                 {                     sql = "select * from  News_type where fid=(select fid from News_type where id=" + f_id + ") and id not in(" + ids + ")  order by fid,sortnum";                     Mys = MyData.ExecuteReader(sql);                     if (Mys.Read())                     {                         if (Mys["fid"].ToString() == "0")                         {                             break;                         }

                        for (j = 1; j <= tempi * 2; j++)                         {                             str = str + " ";                         }                         News_lb.Items.Insert(i, new ListItem(str + MyData.DelHTML(Mys["ClassName"].ToString()), Mys["id"].ToString()));                         i = i + 1;                         ids = ids + "," + Mys["id"].ToString();                         f_id = Mys["id"].ToString();                         str = "";

                    }                     else                     {                         tempi = tempi - 1;                         sql = "select * from News_type where id=" + f_id + " order by fid,sortnum";                         frs = MyData.ExecuteReader(sql);                         if (frs.Read())                         {                             f_id = frs["fid"].ToString();                             if (f_id == "0")                             {                                 break;                             }

                        }                         frs.Close();                     }                     Mys.Close();

                }                 else                 {

                    tempi = tempi + 1;                     for (j = 1; j <= tempi * 2; j++)                     {                         str = str + " ";                     }

                    News_lb.Items.Insert(i, new ListItem(str + MyData.DelHTML(oRs["ClassName"].ToString()), oRs["id"].ToString()));                     str = "";                     i = i + 1;                     f_id = oRs["id"].ToString();                     ids = ids + "," + oRs["id"].ToString();

                }                 oRs.Close();

            } while (1 == 1);

        }         Rs.Close();

    }     //为下拉框填充数据无限级分类,用循环完成

你可能感兴趣的:(C# 用循环来遍历无限级分类)